面试题:(考察Object.defineProperty(obj,prop,descriptor) 的get方法)

参考链接1 javascript学习(九)对象属性的特性

参考链接2https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty


案例1:

var person = {};
Object.defineProperty(person, "name", {
    value:"Tom",
    writable:false,
    enumerable:false,
    configurable:false
});
console.log(person.name);   //Tom
person.name = "Linda";
console.log(person.name);   //Tom

案例2:

var person = {};
Object.defineProperty(person, "name", {
    value:"Tom",
    writable:true,
    enumerable:false,
    configurable:true
});
console.log(person.name);   //Tom
person.name = "Linda";
console.log(person.name);   //Linda

图解:
这里写图片描述

案例3:官方文档解释的太好了!!!

Writable attribute

When the writable property attribute is set to false, the property is said to be “non-writable”. It cannot be reassigned.

var o = {}; // Creates a new object

Object.defineProperty(o, 'a', {
  value: 37,
  writable: false
});

console.log(o.a); // logs 37
o.a = 25; // No error thrown
// (it would throw in strict mode,
// even if the value had been the same)
console.log(o.a); // logs 37. The assignment didn't work.

As seen in the example, trying to write into the non-writable property doesn’t change it but doesn’t throw an error either.


以上内容作为个人学习记录使用,仅供参考,不足之处,烦请告知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值