function People(name){ this.name = name;
} People.prototype.showName = function(){ console.log(this.name); }
function Student(){ } Student.prototype = new People("张三")
Student.prototype.study = function(){ console.log("学习"); } var stu = new Student(); stu.study(); stu.showName();
// console.dir(stu.__proto__); // console.dir(Student.prototype); // console.dir(stu.__proto__===Student.prototype); //console.dir(Student.prototype.__proto__===people.prototype); //console.dir(stu.__proto__.__proto__.__proto__===people.prototype);//原型链 最终指向是原型 object原型是null //console.dir(people.prototype.__proto__.__proto__); //结果null console.dir(stu.__proto__.__proto__.__proto__.__proto__); // Student.prototype . people.prototype . object.prototype . null
继承
<script> function Car(name,speed){ this.name = name; this.speed = speed; // this.showName = function(){ // console.log(this.name) // } // Car.prototype相当于一个数的继承 // JavaScript prototype(原型对象) Car.prototype.showName = function(){ console.log(this.name) } }var car = new Car("奥迪",300); var car2 = new Car("奔驰",280); console.dir(car); console.dir(car2); // car2.showName();
</script>