ES5新特性
JSON
-
var jsonstr=JSON.stringify(obj)
-
var jsonObj=JSON.parse(str)
Object对象的拓展defineProperty
- 这种添加属性默认是不能被修改的(需要设置writeable参数)不能被遍历(需要设置enumerable)不能被删除(需要设置configurable)
var person={}
Object.defineProperties(personm,{
_age:""
name:{
value:"tom",
writable:true,
enumerable:true,
configurable:true,
get:function(){
console.log("get is called")
//产生死循环 return this.age
return this._age
},
set:function(data){
console.log("set is called")
this._age=data
}
}
})
Object拓展create(使用现有对象作为新对象的原型)
var obj={
_age:0,
printInfo:function(){
console.log(`name is ${this.name}`)
}
}
var person=Object.create(obj)
console.log("person",person)
数组的拓展
--------------indexOf
var array=["java","php","javascript","node.js","vue.js"]
console.log(array.indexOf("c#"))
console.log(array.lastIndexOf("php"))
--------------forEach
array.forEach(function(item,index){
console.log(item,index)
})
---------------filter(需要定义一个新的数组接收,原数组没有变化)
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
---------------map(需要定义一个新的数组接收,原数组没有变化)
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
Function函数的扩展(apply,call,bind)
call :
注意:该方法的语法和作用与 apply() 方法类似,只有一个区别,就是 call() 方法接受的是一个参数列表,而 apply() 方法接受的是一个包含多个参数的数组。
---------------------
bind返回一个新的函数而且并不会主动执行,需要手动调用
var ret=fun.bind(obj)
ret(3,4)
或者:
fun.bind(obj)()

浙公网安备 33010602011771号