javascript中Object部分函数

121 篇文章 3 订阅
const log = console.log;

function Animal() {
    this.aniName = "name animal";

    Object.defineProperties(this, {
        "key1": { value: "value1", enumerable: true },
        "key2": { value: "value2", enumerable: false },
        [Symbol.for("symkey1")]: { value: "symbvalue1", enumerable: true },
        [Symbol.for("symkey2")]: { value: "symbvalue2", enumerable: false }
    });
}

Animal.prototype.pinfoAnimal = function() {
    return `i am ${this.aniName}`;
}

function Cat() {
    Animal.call(this);
    this.catName = "name cat";
}

Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.pinfoCat = function() {
    return `i am ${this.catName}`;
}
Cat.prototype.constructor = Cat;

function RedCat() {
    Cat.call(this);
    this.redCatName = "name red cat";
}
RedCat.prototype = Object.create(Cat.prototype);
RedCat.prototype.constructor = RedCat;

let rc = new RedCat();
log("rc = ", rc);
log("RedCat = ", RedCat);

for (let k in rc) {
    log("in k = ", k, ", v = ", rc[k]);
}

log("rc.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(rc));
log("getOwnPropertyNames  = ", Object.getOwnPropertyNames(rc));
log("getOwnPropertySymbols = ", Object.getOwnPropertySymbols(rc));
log("entries = ", Object.entries(rc));
for (let [k, v] of Object.entries(rc)) {
    log("entries k = ", k, ", v = ", v);
}

log("getPrototypeOf = ", Object.getPrototypeOf(rc));

let old = { "k1": "v1" };
old.run = function() {
    return `old-k1: ${this.k1}`;
}

let n = Object.create(old);
log("old = ", old);
log("n = ", n);
for (let k in n) {
    log("n k=", k, ", v = ", n[k]);
}

log("old.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(old));
log("n.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(n));
for (let k in n) {
    log("n.getOwnPropertyDescriptor(", k, ")=", Object.getOwnPropertyDescriptor(n, k));
}

结果:

$ node for.js
rc =  RedCat {
  aniName: 'name animal',
  key1: 'value1',
  catName: 'name cat',
  redCatName: 'name red cat',
  [Symbol(symkey1)]: 'symbvalue1'
}
RedCat =  [Function: RedCat]
in k =  aniName , v =  name animal
in k =  key1 , v =  value1
in k =  catName , v =  name cat
in k =  redCatName , v =  name red cat
in k =  constructor , v =  [Function: RedCat]
in k =  pinfoCat , v =  [Function]
in k =  pinfoAnimal , v =  [Function]
rc.getOwnPropertyDescriptors =  {
  aniName: {
    value: 'name animal',
    writable: true,
    enumerable: true,
    configurable: true
  },
  key1: {
    value: 'value1',
    writable: false,
    enumerable: true,
    configurable: false
  },
  key2: {
    value: 'value2',
    writable: false,
    enumerable: false,
    configurable: false
  },
  catName: {
    value: 'name cat',
    writable: true,
    enumerable: true,
    configurable: true
  },
  redCatName: {
    value: 'name red cat',
    writable: true,
    enumerable: true,
    configurable: true
  },
  [Symbol(symkey1)]: {
    value: 'symbvalue1',
    writable: false,
    enumerable: true,
    configurable: false
  },
  [Symbol(symkey2)]: {
    value: 'symbvalue2',
    writable: false,
    enumerable: false,
    configurable: false
  }
}
getOwnPropertyNames  =  [ 'aniName', 'key1', 'key2', 'catName', 'redCatName' ]
getOwnPropertySymbols =  [ Symbol(symkey1), Symbol(symkey2) ]
entries =  [
  [ 'aniName', 'name animal' ],
  [ 'key1', 'value1' ],
  [ 'catName', 'name cat' ],
  [ 'redCatName', 'name red cat' ]
]
entries k =  aniName , v =  name animal
entries k =  key1 , v =  value1
entries k =  catName , v =  name cat
entries k =  redCatName , v =  name red cat
getPrototypeOf =  RedCat { constructor: [Function: RedCat] }
old =  { k1: 'v1', run: [Function] }
n =  {}
n k= k1 , v =  v1
n k= run , v =  [Function]
old.getOwnPropertyDescriptors =  {
  k1: { value: 'v1', writable: true, enumerable: true, configurable: true },
  run: {
    value: [Function],
    writable: true,
    enumerable: true,
    configurable: true
  }
}
n.getOwnPropertyDescriptors =  {}
n.getOwnPropertyDescriptor( k1 )= undefined
n.getOwnPropertyDescriptor( run )= undefined

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值