ECMAScript中的所有的类都由Object类继承而来,Object类中的所有属性和方法都会出现在其他类中。这里介绍几个Object类相关的属性,方法和操作符;
1.Prototype属性
Prototype — 通过构造函数而创建的对象实例的原型对象.所有的类,默认返回 Object 对象的一个实例,原型对象可以让所有的对象实例共享它的属性和方法。例子见下面几个例子。
2.constructor属性
一个函数创建的时候就会js引擎就会自动为这个函数创建一个prototype属性,默认情况下,prototype属性会自动获取一个constructor属性,这个属性包含一个指向prototype属性所在函数的指针,也就是构造函数。
例如:
function Person(name,age){
this.name = name;
this.age=age;
this.sayName=function(){
console.log(this.name);
}
}
var person1=new Person("码头",31);
console.log(person1.constructor==Person); //true
console.log(person1.constructor==Object); //false
还有一种特殊情况:
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
console.log(person1.constructor==Person); //false
console.log(person1.constructor==Object); //true
这里Person.prototype={}的写法完全重写了Person的prototype属性,所以 prototype中原先的constructor也变成了新对象的constructor,因为“{}”相对于new Object(); 指向Object构造函数
3.instanceof操作符
instanceof操作符用于检查指定对象是否为特地对象的实例。例如:
function Person(name,age){
this.name = name;
this.age=age;
this.sayName=function(){
console.log(this.name);
}
}
var person1=new Person("码头",31);
console.log(person1 instanceof Person); //true
console.log(person1 instanceof Object); //true
console.log(person1 instanceof Function); //false
4.in操作符
in 操作符会在通过对象能够访问给定属性时返回true,即只要是对象的属性,不管来自实例的还是来自原型的,都将返回true。例如
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sex:"man",
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
var person2=new Person("愚人码头",31);
person1.name = "feiwen8772" ;
//person1.sayName();
console.log("name" in person1); //true 来自实例
console.log("name" in person2); //true 来自实例
console.log("sex" in person2); //true 来自原型
5.isPrototypeOf()
isPrototypeOf函数方法是返回一个布尔值,指出对象是否存在于给定对象的原型链中。
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
console.log(Person.prototype.isPrototypeOf(person1)); //true
6.hasOwnPrototype()
hasOwnPrototype()方法可以检测一个属性是否存在于对象实例中。
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sex:"man",
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
var person2=new Person("愚人码头",31);
person1.name = "feiwen8772" ;
//person1.sayName();
console.log(person1.hasOwnProperty("name")); //true 来自实例
console.log(person2.hasOwnProperty("name")); //true 来自实例
console.log(person2.hasOwnProperty("sex")); //false 来自原型
1.Prototype属性
Prototype — 通过构造函数而创建的对象实例的原型对象.所有的类,默认返回 Object 对象的一个实例,原型对象可以让所有的对象实例共享它的属性和方法。例子见下面几个例子。
2.constructor属性
一个函数创建的时候就会js引擎就会自动为这个函数创建一个prototype属性,默认情况下,prototype属性会自动获取一个constructor属性,这个属性包含一个指向prototype属性所在函数的指针,也就是构造函数。
例如:
function Person(name,age){
this.name = name;
this.age=age;
this.sayName=function(){
console.log(this.name);
}
}
var person1=new Person("码头",31);
console.log(person1.constructor==Person); //true
console.log(person1.constructor==Object); //false
还有一种特殊情况:
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
console.log(person1.constructor==Person); //false
console.log(person1.constructor==Object); //true
这里Person.prototype={}的写法完全重写了Person的prototype属性,所以 prototype中原先的constructor也变成了新对象的constructor,因为“{}”相对于new Object(); 指向Object构造函数
3.instanceof操作符
instanceof操作符用于检查指定对象是否为特地对象的实例。例如:
function Person(name,age){
this.name = name;
this.age=age;
this.sayName=function(){
console.log(this.name);
}
}
var person1=new Person("码头",31);
console.log(person1 instanceof Person); //true
console.log(person1 instanceof Object); //true
console.log(person1 instanceof Function); //false
4.in操作符
in 操作符会在通过对象能够访问给定属性时返回true,即只要是对象的属性,不管来自实例的还是来自原型的,都将返回true。例如
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sex:"man",
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
var person2=new Person("愚人码头",31);
person1.name = "feiwen8772" ;
//person1.sayName();
console.log("name" in person1); //true 来自实例
console.log("name" in person2); //true 来自实例
console.log("sex" in person2); //true 来自原型
5.isPrototypeOf()
isPrototypeOf函数方法是返回一个布尔值,指出对象是否存在于给定对象的原型链中。
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
console.log(Person.prototype.isPrototypeOf(person1)); //true
6.hasOwnPrototype()
hasOwnPrototype()方法可以检测一个属性是否存在于对象实例中。
function Person(name,age){
this.name = name;
this.age=age;
}
Person.prototype={
sex:"man",
sayName:function(){
console.log(this.name);
}
};
var person1=new Person("码头",31);
var person2=new Person("愚人码头",31);
person1.name = "feiwen8772" ;
//person1.sayName();
console.log(person1.hasOwnProperty("name")); //true 来自实例
console.log(person2.hasOwnProperty("name")); //true 来自实例
console.log(person2.hasOwnProperty("sex")); //false 来自原型