??????????????bug?????????????????????????????????????????????????????
????for in ?????????????????bug
??????? for in ?????????????????????????????????????£????????????????????????Щ???????????? ??????? ???? ???????????? ????????????????????????????Щ???????( [[Enumerable]] )? true ???????
????1???????????????????????????????????????????? true
????2????????? Object.defineProperty ???????????????????? false
?????????????Щ???????????
let Person = function(name?? sex){
this.name = name
this.sex = sex
}
Person.prototype = {
constructor: Person??
showName () {
console.log(this.name)
}??
showSex () {
console.log(this.sex)
}
}
Person.wrap = {
sayHi () {
console.log('hi')
}
}
var p1 = new Person('qianlongo'?? 'sex')
p1.sayBye = ()=> {
console.log('bye')
}
p1.toString = ()=> {
console.log('string')
}
Object.defineProperty(p1?? 'info'?? {
enumerable: false??
configurable: false??
writable: false??
value: 'feDev'
});?
for (var key in p1) {
console.log(key)
}
// name
// sex
// sayBye
// constructor
// showName
// showSex
// toString
????1????????????????????defineProperty??????????????????????enumerable?false??????????????????
????2??Person?????Object???????????? for in ?????????Object???????Щ????
????3???????????????????????? toString ????????
????????ж????????????????????
?????????????????????js???? Object.propertyIsEnumerable ???ж?
let obj = {
name: 'qianlongo'
}
let obj2 = {
name: 'qianlongo2'??
toString () {
return this.name
}
}
obj.propertyIsEnumerable('name') // true
obj.propertyIsEnumerable('toString') // false
obj2.propertyIsEnumerable('name') // true
obj2.propertyIsEnumerable('toString') // true
???????obj?ж?toString???????????????obj2???????????????????obj2?? toString ??д???????????????????????????????????
?????????????????????????????????????漰??????????????????come on??????
????_.has(object?? key)
?????ж????obejct??????key????
????????????????????ж?????????????????????
????if (obj && obj.key) {
????// xxx
????}
???????????????????????????????????????0??null??false??’’??????????????????obj??????????????????????????????????????
????let obj = {
????name: ''??
????sex: 0??
????handsomeBoy: false??
????timer: null
????}
????????????????????????е???????
???????
????var hasOwnProperty = ObjProto.hasOwnProperty;
????_.has = function(obj?? key){
????return obj != null && hasOwnProperty.call(obj?? key);
????};
????_.keys(object)
???????object???????е??????????
??????????
????let obj = {
????name: 'qianlongo'??
????sex: 'boy'
????}
????let keys = _.keys(obj)
????// ["name"?? "sex"]
???????
_.keys = function(obj){
// ???obj????object???????????????
if (!_.isObject(obj)) return [];
// ????????????????keys????????????????keys
if (nativeKeys) return nativeKeys(obj);
var keys = [];
// ???????1??for in????????????????????_.has???????????????????????
for (var key in obj) if (_.has(obj?? key)) keys.push(key);
// Ahem?? IE < 9.
// ???????????ie9???μ????????bug???????????Щ???????????????????????????????collectNonEnumProps????
if (hasEnumBug) collectNonEnumProps(obj?? keys);
return keys;
};
????collectNonEnumProps????????
?????ú??????????е??????????????????ie9???μ????bug????
???????
// ?ж???????????????bug??????У???????????????false
var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
// ??????????????????????????????
var nonEnumerableProps = ['valueOf'?? 'isPrototypeOf'?? 'toString'??
'propertyIsEnumerable'?? 'hasOwnProperty'?? 'toLocaleString'];
// ????ie9???μ???????bug
functioncollectNonEnumProps(obj?? keys){
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
// ???obj?????
var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto;
// ???????и??????????constructor?????????????????
// Constructor is a special case.
var prop = 'constructor';
if (_.has(obj?? prop) && !_.contains(keys?? prop)) keys.push(prop);
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
// nonEnumerableProps?е??????????obj?У??????????е?????????????????keys?в????????????????
if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys?? prop)) {
keys.push(prop);
}
}
}
??????????????????????????????С?????????constructor??????????????????????????????????????????
????_.allKeys(object)
???????object?????е?????????????????
??????????????????
let Person = function(name?? sex){
this.name = name
this.sex = sex
}
Person.prototype = {
constructor: Person??
showName () {
console.log(this.name)
}
}
let p = new Person('qianlongo'?? 'boy')
_.keys(p)
// ["name"?? "sex"] ??????????????
_.allKeys(p)
// ["name"?? "sex"?? "constructor"?? "showName"] ????????????????
?????????????????????????
???????
// ???????obj?????е??
// ??keys???????????????????key
// Retrieve all the property names of an object.
_.allKeys = function(obj){
if (!_.isObject(obj)) return [];
var keys = [];
// ?????????????key????????????
for (var key in obj) keys.push(key);
// Ahem?? IE < 9.
if (hasEnumBug) collectNonEnumProps(obj?? keys); // ?????????????????????????
return keys;
};
?????????????_.keys????????????obj???????????? hasOwnProperty ??ж?