标签:esc 方法 面试题 als highlight call filter data 实参
一:Object扩展属性
1. Object.create(prototype, [descriptors])
* 作用: 以指定对象为原型创建新的对象
* 为新的对象指定新的属性, 并对属性进行描述
value : 指定值
writable : 标识当前属性值是否是可修改的, 默认为false
configurable: 标识当前属性是否可以被删除 默认为false
enumerable: 标识当前属性是否能用for in 枚举 默认为false
2. Object.defineProperties(object, descriptors)
* 作用: 为指定对象定义扩展多个属性
* get :用来获取当前属性值得回调函数
* set :修改当前属性值得触发的回调函数,并且实参即为修改后的值
* 存取器属性:setter,getter一个用来存值,一个用来取值。
var obj = {name : ‘curry‘, age : 29}
var obj1 = {};
obj1 = Object.create(obj, {
sex : {
value : ‘男‘,//设置数值
writable : true //权限能否修改
}
});
obj1.sex = ‘女‘;
console.log(obj1.sex);
//Object.defineProperties(object, descriptors)
var obj2 = {
firstName : ‘curry‘,
lastName : ‘stephen‘
};
Object.defineProperties(obj2, {
fullName : {
get : function () {
return this.firstName + ‘-‘ + this.lastName
},
set : function (data) {
var names = data.split(‘-‘);
this.firstName = names[0];
this.lastName = names[1];
}
}
});
console.log(obj2.fullName);//获取扩展的属性自动调用get方法
obj2.firstName = ‘tim‘;
obj2.lastName = ‘duncan‘;
console.log(obj2.fullName);
obj2.fullName = ‘kobe-bryant‘;//更改属性自动调用set方法
console.log(obj2.fullName);
对象本身的两个方法
* get propertyName(){} 用来得到当前属性值的回调函数
* set propertyName(){} 用来监视当前属性值变化的回调函数
<script type=‘text/javascript‘>
var obj = {
firstName : ‘kobe‘,
lastName : ‘bryant‘,
get fullName(){
return this.firstName + ‘ ‘ + this.lastName
},
set fullName(data){
var names = data.split(‘ ‘);
this.firstName = names[0];
this.lastName = names[1];
}
};
console.log(obj.fullName);
obj.fullName = ‘curry stephen‘;
console.log(obj.fullName);
</script>
二:数组的扩展属性
1. Array.prototype.indexOf(value) : 得到值在数组中的第一个下标
2. Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
3. Array.prototype.forEach(function(item, index){}) : 遍历数组
4. Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
5. Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
var arr = [1, 4, 6, 2, 5, 6];
console.log(arr.indexOf(6));//2
//Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
console.log(arr.lastIndexOf(6));//5
//Array.prototype.forEach(function(item, index){}) : 遍历数组
arr.forEach(function (item, index) {
console.log(item, index);
});
//Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
var arr1 = arr.map(function (item, index) {
return item + 10
});
console.log(arr, arr1);
//Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
var arr2 = arr.filter(function (item, index) {
return item > 4
});
console.log(arr, arr2);
三:函数this
1. Function.prototype.bind(obj) :
* 作用: 将函数内的this绑定为obj, 并将函数返回
2. 面试题: 区别bind()与call()和apply()?
* 都能指定函数中的this
* call()/apply()是立即调用函数
* bind()是将函数返回
<script type="text/javascript">
function fun(age) {
this.name = ‘kobe‘;
this.age = age;
console.log(this.age);
}
var obj = {};
fun.apply(obj,[20]);
fun.call(obj,30);
fun.bind(obj,40)();
</script>
标签:esc 方法 面试题 als highlight call filter data 实参
原文地址:https://www.cnblogs.com/love-life-insist/p/9909298.html