本文共 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;//true2.hasOwnPreperty()??
o1.hasOwnProperty('a');//true
o1.hasOwnProperty('b');//true o1.hasOwnProperty('toString');//false3.?? !==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); // true2.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'));//false5.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'));//false6.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???
发表评论
最新留言
关于作者