// 为 Object 设置三个自定义属性(可枚举)
Object.prototype.userProp = 'userProp';
Object.prototype.getUserProp = function() {
return Object.prototype.userProp;
};
// 定义一个对象,隐式地继承自 Object.prototype
var obj = {
name: 'percy',
age: 21,
[Symbol('symbol 属性')]: 'symbolProp',
unEnumerable: '我是一个不可枚举属性',
skills: ['html', 'css', 'js'],
getSkills: function() {
return this.skills;
}
};
// 设置 unEnumerable 属性为不可枚举属性
Object.defineProperty(obj, 'unEnumerable', {
enumerable: false
});
for (let key in obj) {
console.log(key);
console.log(obj.key); // wrong style
console.log(obj[key]); // right style
}
Object.keys(obj); // ["name", "age", "skills", "getSkills"]
Object.getOwnPropertyNames(obj); // ["name", "age", "unEnumerable", "skills", "getSkills"]
Object.getOwnPropertySymbols(obj); // [Symbol(symbol 属性)]
Reflect.ownKeys(obj); // ["name", "age", "unEnumerable", "skills", "getSkills", Symbol(symbol 属性)]
'age' in obj; // true 'userProp' in obj; // true (userProp 是 obj 原型链上的属性) 'name' in Object; // true // 上面这个也是 true 的原因是,Object 是一个构造函数,而函数恰巧也有一个 name 属性 Object.name; // 'Object' Array.name; // 'Array'
obj.hasOwnProperty('age'); // true
obj.hasOwnProperty('skills'); // true
obj.hasOwnProperty('userProp'); // false
// 利用 Object.create() 新建一个对象,并且这个对象没有任何原型链
var obj2 = Object.create(null, {
name: { value: 'percy' },
age: { value: 21 },
skills: { value: ['html', 'css', 'js'] }
});
obj2.hasOwnProperty('name'); // 报错
obj2.hasOwnProperty('skills'); // 报错
Object.prototype.hasOwnProperty.call(obj2,'name'); // true Object.prototype.hasOwnProperty.call(obj2,'skills'); // true Object.prototype.hasOwnProperty.call(obj2,'userProp'); // false
for(let value of arr){
console.log(value);
}
Array.prototype.forEach(callback(currentValue, index, array){
// do something
}[,thisArg]);
// 如果数组在迭代时被修改了,则按照索引继续遍历修改后的数组
var words = ["one", "two", "three", "four"];
words.forEach(function(word) {
console.log(word);
if (word === "two") {
words.shift();
}
});
// one
// two
// four
Array.prototype.map(callback(currentValue, index, array){
// do something
}[,thisArg]);
```
```js
// map 的一个坑
[1,2,3].map(parseInt); // [1, NaN, NaN]
// 提示 map(currentValue,index,array)
// parseInt(value,base)
// 一些相关的案例
// 对数组进行累加、累乘等运算
[1,10,5,3,8].reduce(function(accumulator,currentValue){
return accumulator*currentValue;
}); // 1200
// 数组扁平化
[[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
}); // [0, 1, 2, 3, 4, 5]
[[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}); // [4, 5, 2, 3, 0, 1]
var str = '123,hello';
// 反转字符串
Array.prototype.reduceRight.call(str,function(a,b){
return a+b;
}); // olleh,321
// 过滤字符串,只保留小写字母
Array.prototype.filter.call('123,hello', function(a) {
return /[a-z]/.test(a);
}).join(''); // hello
// 利用 map 遍历字符串(这个例子明显举得不太好 *_*)
Array.prototype.map.call(str,function(a){
return a.toUpperCase();
}); // ["1", "2", "3", ",", "H", "E", "L", "L", "O"]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有