组织函数形式
ECMAScript中的组织函数可用来建立特定范例的对象,像Object和Array如许的原生组织函数。
也能够建立自定义的组织函数,从而定义自定义对象范例的属性和要领。
1.建立对象
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.showName = function(){
console.log("name = " + this.name);
};
}
var p1 = new Person("Mike", 20, "student");
var p2 = new Person("Tom", 23, "student");
console.log(p1);
console.log(p2);
与工场形式的区分:
没有显现地建立对象
直接将要领和属性付给了this对象
没有
return
语句
组织函数应当一直以一个大写字母开首。
建立组织函数的实例,必需运用new
操作符。以这类体式格局挪用组织函数,会阅历以下4个步骤:
建立一个新对象;
将组织函数中的作用域赋给新对象(this就指向了这个对象)
实行组织函数中的代码(为这个新对象增加属性)
返回新对象
对象有一个constructor用来标识对象范例。p1和p2对象,都有一个constructor(组织函数)属性,该属性指向Person。
console.log("p1.constructor=");
console.log(p1.constructor);
console.log("p1.constructor === Person:");
console.log(p1.constructor === Person);
console.log("p2.constructor=");
console.log(p2.constructor);
console.log("p2.constructor === Person:");
console.log(p2.constructor === Person);
然则,提到检测对象范例,照样instanceof
操作符要更牢靠些。Person组织函数建立的一切对象,既是Object的实例,也是Person的实例。
console.log("p1 instanceof Object:");
console.log(p1 instanceof Object);
console.log("p1 instanceof Person:");
console.log(p1 instanceof Person);
console.log("p2 instanceof Object:");
console.log(p2 instanceof Object);
console.log("p2 instanceof Person:");
console.log(p2 instanceof Person);
2.视察Person组织函数涉及到的原型链
console.log("Person.prototype=");
console.log(Person.prototype);
console.log("Person.prototype === Function.prototype;");
console.log(Person.prototype === Function.prototype); //false
console.log("-----分割线-----");
console.log("Person.prototype.constructor === Person:"); //true
console.log(Person.prototype.constructor === Person); //true
console.log("-----分割线-----");
console.log("Person.prototype.__proto__ === Object.prototype:"); //true
console.log(Person.prototype.__proto__ === Object.prototype); //true
console.log("-----分割线-----");
console.log("Person.__proto__=");
console.log(Person.__proto__);
console.log("Person.__proto__ === Function.prototype:");
console.log(Person.__proto__ === Function.prototype); //true
console.log("-----分割线-----");
3.视察p1实例对象涉及到的原型链
console.log("p1.prototype=");
console.log(p1.prototype);
console.log("-----分割线-----");
console.log("p1.__proto__=");
console.log(p1.__proto__);
console.log("p1.__proto__ === Person:");
console.log(p1.__proto__ === Person); //false
console.log("p1.__proto__ === Person.prototype:");
console.log(p1.__proto__ === Person.prototype); //true
console.log("-----分割线-----");
console.log("p1.__proto__.constructor === Person:");
console.log(p1.__proto__.constructor === Person);
console.log("-----分割线-----");
console.log("p1.__proto__.__proto__=");
console.log(p1.__proto__.__proto__);
console.log("p1.__proto__.__proto__ === Object.prototype:");
console.log(p1.__proto__.__proto__ === Object.prototype); //true
console.log("-----分割线-----");
4.视察下p1.showName属性援用的函数
console.log("p1.showName.prototype=");
console.log(p1.showName.prototype);
console.log("p1.showName.prototype === Function.prototype:");
console.log(p1.showName.prototype === Function.prototype); //false
console.log("-----分割线-----");
console.log("p1.showName.prototype.constructor=");
console.log(p1.showName.prototype.constructor);
console.log("-----分割线-----");
console.log("p1.showName.prototype.__proto__=");
console.log(p1.showName.prototype.__proto__);
console.log("p1.showName.prototype.__proto__ === Object.prototype:");
console.log(p1.showName.prototype.__proto__ === Object.prototype); //true
console.log("-----分割线-----");
console.log("p1.showName.__proto__=");
console.log(p1.showName.__proto__);
console.log("p1.showName.__proto__ === Function.prototype:");
console.log(p1.showName.__proto__ === Function.prototype); //true
console.log("-----分割线-----");