Object.defineProperty
var obj = {
test: "hello"
};
obj['favor'] = 'orange';
//对象已有的属性添加特性描述
// Object.defineProperty(obj, "test", {
// configurable: true,
// enumerable: true,
// writable: true
// });
//对象新添加的属性的特性描述
Object.defineProperty(obj, "age", {
configurable: true,
value: 22,
enumerable: true,
writable: false
});
obj['age'] = 10;
console.log(obj);
检测类型Object.prototype.toString.call
Object.prototype.toString.call();
Object.prototype.toString.call([]); //"[object Array]"
Object.prototype.toString.call({}); //"[object Object]"
Object.prototype.toString.call(null); //"[object Null]"
Object.prototype.toString.call(undefined); //"[object Undefined]"
转数组
Array.prototype.slice().call(likeArr);
let likeArrToArr = function (likeArr) {
var arr = [];
try {
arr = Array.prototype.slice.call(likeArr);
} catch (e) {
for (var i = 0; i < likeArr.length; i++) {
arr[arr.length] = likeArr[i];
}
}
return arr;
};
var arr = {length: 2, 0: 'first', 1: 'second'};
console.log(likeArrToArr(arr));
var arr2 = [0,11,22,33];
console.log(likeArrToArr(arr2));