【JavaScript】【对象】属性和要领的总结归结

组织函数、原型对象、实例对象之间的关联扑朔迷离,关于它们的属性和要领许多,长得也很像。这里归结出来,轻易影象和查阅。

对象属性范例

数据属性

[[Configurable]]:示意可否经由过程delete删除属性从而从新定义属性、可否修正属性的特征、可否把属性修正为接见器属性。默认值为true
[[Enumerable]]:示意是不是可罗列。默认值为true
[[Writable]]:示意可否修正属性的值。默认值为true
[[Value]]:包括属性的数据值,在这里读写属性。默认值为undefined

修正属性的特征:Object.defineProperty()Object.defineProperties()。挪用这两个要领时,假如不指定,configurableenumerablewritable特征的默认值都是false

//定义一个属性
var person = {};
Object.defineProperty(person, 'name', {
    value: 'hiyohoo',
    configurable: true,
    enumerable: true,
    writable: true
});

//定义多个属性
var person = {};
Object.defineProperties(person, {
    name: {
        value: 'hiyohoo'
    },
    age: {
        value: 24,
        configurable: true,
        writable: true
    }
});

猎取属性的特征:Object.getOwnPropertyDescriptor()只能用于实例属性,要获得原型属性的描述符,需要在原型上运用该要领。

var descriptor = Object.getOwnPropertyDescriptor(person, 'age');
alert(descriptor.writable);    //true

接见器属性

[[Configurable]]
[[Enumerable]]
[[Get]]:读取属性时挪用该函数。默认值为undefined
[[Set]]:写入属性时挪用该函数。默认值为undefined

接见器属性不能直接定义,必需运用Object.defineProperty()来定义。接见器属性常用于转变该属性,其他属性也会变化的状况。

var book = {
    _year: 2004,    //属性前面的下划线暗号常用于示意只能经由过程对象要领接见的属性。
    edition: 1
};
Object.defineProperty(book, 'year', {
    get: function() {
        return this._year;
    },
    set: function(newValue) {
        if (newValue > 2004) {
            this._year = newValue;
            this.edition += newValue - 2004;
        }
    }
});
book.year = 2016;
console.log(book.edition);    //13

属性和要领

以下的属性和要领均以下面的代码为例:

var Person = function(name) {
    this.name = name;
};
Person.prototype.school = 'HNU';
Person.prototype.sayName = function() {
    return this.name;
};

var person = new Person('hiyohoo');

组织函数

prototype指向原型对象,包括一切被实例同享的属性和要领。

console.log(Person.prototype);    //Person{}

原型对象

constructor指向组织函数。

console.log(Person.prototype.constructor === Person);    //true

isPrototypeOf()推断实例与原型之间的关联。

console.log(Person.prototype.isPrototypeOf(person));    //true

实例对象

constructor沿着原型链找到原型中的constructor属性,终究指向组织函数。

console.log(person.constructor === Person);    //true

__proto__Firefox、Safari、Chrome支撑这个属性,指向原型对象。

console.log(person.__proto__ === Person.prototype);    //true

hasOwnProperty()Object中继续而来,推断属性是不是是实例的私有属性,而不是继续而来的同享属性。

console.log(person.hasOwnProperty('name'));    //true
console.log(person.hasOwnProperty('school'));    //false

Object

Object.getPrototypeOf() ECMAScript 5中新增的要领,返回实例的原型。

console.log(Object.getPrototypeOf(person));    //Person{}

Object.keys() ECMAScript 5中新增的要领,返回一个包括一切可罗列实例属性的字符串数组。

console.log(Object.keys(person));    //["name"]
console.log(Object.keys(Person.prototype));    //["school", "sayName"]

Object.getOwnPropertyNames()返回一切实例属性,不管是不是可罗列。

console.log(Object.getOwnPropertyNames(person));    //["name"]
console.log(Object.getOwnPropertyNames(Person.prototype));    //["constructor", "school", "sayName"]

操作符

delete删除一个configurabletrue的私有属性。

delete person.name;
delete person.school;

console.log(person.name);    //undefined
console.log(person.school);    //HNU

for-in返回一切能够接见到的属性。

for (p in person) {
    console.log(p);    //name school sayName
}

in对象能够接见到属性时返回true

console.log('name' in person);    //true
console.log('sayName' in person);    //true
console.log('age' in person);    //false

同时运用hasOwnProperty()要领和in操作符,能够肯定一个属性是存在于对象中照样存在于原型中。

function isPrototypeProperty(object, name) {
    if (!(name in object)) {
        return ("Can't find " + '"' + name + '"');
    } else if (object.hasOwnProperty(name)) {
        return false;
    } else {
        return true;
    }
}

console.log(isPrototypeProperty(person, 'name'));    //false
console.log(isPrototypeProperty(person, 'school'));    //true
console.log(isPrototypeProperty(person, 'age'));    //Can't find "age"

instanceof用于推断一个对象是不是是某个对象的实例。

console.log(person instanceof Person);    //true
console.log(person instanceof Object);    //true

转载请说明出处:https://segmentfault.com/a/1190000004561741

文章不定期更新完美,假如能对你有一点点启示,我将不胜幸运。

    原文作者:hiYoHoo
    原文地址: https://segmentfault.com/a/1190000004561741
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞