You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function Person(name, age) {
this.name = name
this.age = age
}
var me = new Person('matthew', 22)
var proto = Object.getPrototypeOf(me)
proto.sex = 'male'
console.log(me.sex)
console.log(Person.prototype.sex)
console.log(proto === Person.prototype)
console.log(proto.isPrototypeOf(me))
// output
// male
// male
// true
// true
验证数据类型:
var arr = []
var result = (Object.getPrototypeOf(arr) === Array.prototype)
console.log(result)
// true
扩展,过去验证数据类型:
var arr = []
var result = (Object.prototype.toString.call(arr) === '[object Array]')
console.log(result)
// true
Object.create(prototype[, descriptors])
描述:
创建一个具有指定原型且可选择性地包含指定属性的对象。
参数:
prototype: 必需。对象的原型链,可以为null
descriptors: 可选。包含一个或多个属性描述符的 JavaScript 对象。
示例:
创建一个普通对象模型
创建一个以null为对象原型,并添加一些属性描述。
Object.defineProperty(object, propertyname, descriptor)
描述:
将属性添加到对象或修改现有属性的特性。
参数:
object: 必需。对其添加或修改属性的对象。这可以是本机 JavaScript 对象(即用户定义的对象或内置对象)或 DOM 对象。
propertyname:一个包含属性名称的字符串。
descriptor:必需。属性的描述符。它可以针对数据属性或访问器属性。
示例:
添加数据属性
假如修改writable值为false,则输出的value为101
假如修改configurable值为false,则不能使用delete obj.newDataProperty
修改数据属性
添加访问器属性
请注意这里并没有对newAccessorProperty属性设置value值和writable属性,全靠get/set对数据属性进行了访问,假如删去了get/set,将会返回undefined。
修改访问器属性
修改DOM元素上的属性
请注意此例子页面中必须包含id为div的元素。
Object.defineProperties(object, descriptors)
描述:
将一个或多个属性添加到对象,并/或修改现有属性的特性。
参数
object: 必需。对其添加或修改属性的对象。这可以是本机 JavaScript 对象(即用户定义的对象或内置对象)或 DOM 对象。
descriptors:必需。包含一个或多个描述符对象的 JavaScript 对象。 每个描述符对象描述一个数据属性或访问器属性。
示例
添加属性
修改属性
Object.getPrototypeOf(object)
描述:
返回对象的原型
参数:
object:必须。引用原型的对象。
示例:
验证数据类型:
扩展,过去验证数据类型:
Object.keys(object)
描述:
返回对象的可枚举属性和方法的名称。
参数
object:必需。包含属性和方法的对象。这可以是您创建的对象或现有文档对象模型 (DOM) 对象。
示例
回顾一下知识,过去是如何获取键值?
Object.seal(object)
描述:
阻止修改现有属性的特性,并阻止添加新属性。
参数:
object:必需。在其上锁定特性的对象。
备注:
Object.seal 函数执行以下两项操作:
示例:
Object.freeze(object)
描述:
阻止修改现有属性的特性和值,并阻止添加新属性。
参数:
object:必需。在其上锁定特性的对象。
备注
Object.freeze 函数执行下面的操作:
示例:
Object.preventExtensions(object)
描述:
阻止向对象添加新属性。
参数:
object:必需。要成为不可扩展的对象的对象。
示例:
Object.isSealed(object)
描述:
如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回 true。
Object.isFrozen(object)
描述:
如果无法在对象中修改现有属性的特性和值,且无法向对象添加新属性,则返回 true。
Object.isExtensible(object)
描述:
返回一个值,该值指示是否可向对象添加新属性。
Object.getOwnPropertyDescriptor(object, propertyname)
描述:
获取指定对象自己的属性描述符。 自己的属性描述符是直接在对象上定义的描述符,而不是从对象的原型继承的描述符。
参数:
object:必需。包含该属性的对象。
propertyname:必需。属性的名称。
示例:
Object.getOwnPropertyNames(object)
描述:
返回对象自己的属性的名称。一个对象的自己的属性是指直接对该对象定义的属性,而不是从该对象的原型继承的属性。对象的属性包括字段(对象)和函数。
参数
object:必需。包含自己的属性的对象。
返回值:
一个数组,其中包含对象自己的属性的名称。
示例:
关于ECMA5,object的介绍就到这么多了,下面会陆续写关于Date,Json,Function,String,Array等的介绍,还请大家感兴趣的多多关注。
如果有任何问题都可以在下方给予我留言~
The text was updated successfully, but these errors were encountered: