构造函数、原型链:function Person(name, age, job) { this.name = name; this.age = age; this.job = job;// this.sayName = function () {/ ...
构造函数、原型链:
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
原标题:JavaScript 构造函数与原型链
关键词:JavaScript
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。