構造函數、原型鏈: ...
構造函數、原型鏈:
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; // this.sayName = function () { // console.log(this.name); // }; Person.prototype.sayName = function () { console.log(this.name); } } var p1 = new Person('Jesse', 18, 'coder'); var p2 = new Person('Carol', 17, 'designer'); console.log(p1.sayName==p2.sayName); //this.sayName = function () {} // false // console.log(p1.sayName==p2.sayName); //Person.prototype.sayName = function () {} // true console.log(p1.sayName()); //console.log(name); // Carol // console.log(p1.sayName()); //console.log(this.name); // Jesse