JavaScript对象详解,深入理解js对象
发布日期:2021-04-30 21:11:27 浏览次数:89 分类:精选文章

本文共 3481 字,大约阅读时间需要 11 分钟。

??

???JavaScript?????????????????????????????????????????????????????????????????????????????????????

????

1.?????

var empty={} //???????

var obj={a:1,b:2};//???????
var compObj={a:'1',b:'2',cc:{d:'3',e:'4'}};//?????

2.new???????????

var a = new Array();//???????

var d = new Date();//???????Date??

3.??????

function Person(name){this.name=name;}

Person.prototype.eat=function(){console.log(this.name+"????");}
var zhangsan = new Person("??");

4.Object.create()

var o1=Object.create({a:1,b:2});//o1????? a?b

????????

?????(.)????([])???????

var o1={a:1,b:2,'hello zhangsan':'??'}

o1.a //1
o1.b //2
o1['a']//1
o1['b']//2
var key='hello zhangsan';
o1[key]//??
o1.key//undefined
o1.hello zhangsan //??

??? (.) ?????????????????????????????????????????????????????????????????

??????????????????

o1.a='3'

o1['b']='4'

????

delete o1.a; delete o1['b']

???delete?????????????????????

????

1.in???

'a' in o1;//true

'b' in o1;//true
'toString' in o1;//true

2.hasOwnPreperty()??

o1.hasOwnProperty('a');//true

o1.hasOwnProperty('b');//true
o1.hasOwnProperty('toString');//false

3.?? !==undefined

o1.a!==undefined;//true

o1.b!==undefined;//true
o1.toString!==undefined;// ???????true

??????????undefined????????????? var o1={a:undefined} ,??a????????????

?????

1.for/in??

for(key in o1){console.log(key,'===',o1[key])}

2.Object.keys ??

var keys = Object.keys(o1);

keys.forEach((key)=>{console.log(key,'===',o1[key])}

????

???__proto__??????????prototype?

prototype??constructor ???????????
isPrototypeOf?????????????????????????????

?????

1.valueOf() ????????????

Array?????????

var array = [1,3,4,6];console.log(array.valueOf() === array); // true
Number??????
var num = 123213;console.log(num.valueOf()); // 15.2654
boolean??????true?false
var bool = true;console.log(bool.valueOf() === bool); // true

2.toString() ???????????????

?????????????????????

var toString = Object.prototype.toString; console.log(toString.call(new Object()));//[object Object]
console.log(toString.call(new Date())); // [object Date]
console.log(toString.call('abc')); // [object String]
console.log(toString.call(123)); // [object Number]
console.log(toString.call(true)); // [object Boolean]
console.log(toString.call(undefined)); // [object Undefined]
console.log(toString.call(null)); // [object Null]
console.log(toString.call(/./));//[object RegExp]

3.toLocaleString() ???????????????

var toString = Object.prototype.toLocaleString; console.log(toString.call(new Object()));//[object Object]

console.log(toString.call(new Date())); // Wed Dec 23 2020 20:09:24 GMT+0800 (??????)
console.log(toString.call('abc')); // abc
console.log(toString.call(123)); // 123
console.log(toString.call(true)); // true
console.log(toString.call(/./));// /./

4.hasOwnProperty() ??????????

var o1={a:1,b:2,'hello zhangsan':'??'}

console.log(o1.hasOwnProperty('a'));//true
console.log(o1.hasOwnProperty('b'));//true
console.log(o1.hasOwnProperty('toString'));//false

5.propertyIsEnumerable() ??

var o1={a:1,b:2,'hello zhangsan':'??'}

console.log(o1.propertyIsEnumerable('a'));//true
console.log(o1.propertyIsEnumerable('b'));//true
console.log(o1.propertyIsEnumerable('toString'));//false

6.isPrototypeOf() ??

function Person(){}

function Man(){}
Man.prototype = Object.create(Person.prototype);
var m = new Man();
console.log(Person.prototype.isPrototypeOf(m)); // true
console.log(Object.prototype.isPrototypeOf(m)); // true

???????? m ?????__proto__???Person.prototype?????Person.prototype???__proto__?????Object.prototype???

上一篇:Unresolved reference ‘xrange‘ 解决方法。
下一篇:“码农”过了35岁还有价值吗?经验在手你没路走!

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2026年06月10日 07时13分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章