你的位置:首页 > Java教程

[Java教程]JavaScript 构造函数与原型链


构造函数、原型链:

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