读书笔记——《You Don't Know JS》

第一部:《You don't know JS: this & Object prototype》

第三章 Object

对象常量

var myObject = {};
Object.defineProperty( myObject, "FAVORITE_NUMBER", {
 value: 42,
 writable: false,
 configurable: false
} );

禁止对象扩展

var myObject = {
 a: 2
};
Object.preventExtensions( myObject );
myObject.b = 3;
myObject.b; // undefined

   使用preventExtensions方法,可禁止对象再次扩展。 

Object.seal(..)
     preventExtensions + configurable:false

Object.freeze(..)
     s Object.seal(..) + writable:false

对象默认方法[[Get]]、[[PUT]]

自定义Getters和Setters

var myObject = {
 // define a getter for `a`
 get a() {
 return 2;
 }
};
Object.defineProperty(
 myObject, // target
 "b", // property name
 { // descriptor
 // define a getter for `b`
 get: function(){ return this.a * 2 },
 // make sure `b` shows up as an object property
 enumerable: true
 }
);
var myObject = {
 // define a getter for `a`
 get a() {
 return this._a_;
 },
 // define a setter for `a`
 set a(val) {
 this._a_ = val * 2;
 }
};

 

转载于:https://www.cnblogs.com/javawer/p/3904363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值