楼主今天主要分享一下ES5的一些补充内容,后面会继续写ES6的部分,希望大家多多点赞支持!
'use strict'; // 开启严格模式
var username = 'kobe';
var age = 43;
console.log(username, age);
// 禁止自定义的函数中的this指向window
function Person(name) {
this.name = name;
}
new Person('kobe');
// 创建eval作用域,可以接卸传入的字符串并进行解析
var a = 2;
eval('var a = 123; alert(a)')
console.log(a);
// 对象 不能有重名的属性
var obj = {
name: 'kobe',
name: 'wade'
}
console.log(obj.name);
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);
obj2.firstName = 'tim';
obj2.lastName = 'duncan';
console.log(obj2.fullName);
obj2.fullName = 'kobe-bryant';
console.log(obj2.fullName);
var arr = [1, 2, 3, 4, 5, 2, 2, 3];
console.log(arr.indexOf(2));
console.log(arr.lastIndexOf(3));
var newArr = arr.map(function(item, index) {
// console.log(item, index);
// return 返回加工之后的每一项
return item + 10;
})
console.log(newArr);
console.log(arr);
// filter
var newArr2 = arr.filter(function(item, index) {
return item > 3;
})
console.log(newArr2);
var arr3 = [1, 2, 3];
arr3.push(4);
console.log(arr3);
arr3.forEach(function(item, index) {
console.log(item, index);
})
var obj = {
name: ‘kobe’
}
function fun(msg) {
console.log(this, msg);
}
// fun.call(obj, 123);
// fun.apply(obj, [123]);
// bind修改this指向
fun.bind(obj, 123)();
setTimeout(function () {
console.log(this);// window
}.bind(obj), 1000)