zoukankan      html  css  js  c++  java
  • JS ES5

    常用

    严格模式 use strict

    • 必须使用var声明变量
    • 禁止自定义函数this指向window
    'use strict'
    funcion Person(name){
        this.name = name;
    }
    Person("Tom"); //error
    new Person("Tom"); //right
    
    • 为eval创建作用域
    • 对象属性名不能重复

    JSON对象

    • JSON.stringify(obj/arr) js对象(数组)转json对象(数组)
    • JSON.parse(json) json对象转(数组)js对象(数组)

    Object扩展

    • Object.create(prototype, [descriptors]) 以指定对象为原型创建新的对象
    var man = {sex:'mail'};
    var person = Object.create(man, {
    	name: {
    		value: '',
    		writable: true, //可写
    		configurable: true, //可配置(可删除)
    		enumerable: true //可枚举
    	}
    })
    person.name = 'Tom';
    for (let value in person) {
    	console.log(value); //name sex
    }
    
    • Object.defineProperties(object, descriptors) 为指定对象定义扩展多个属性
    var obj1 = {
    	firstName: 'Kevin',
    	lastName: 'Tseng'
    };
    Object.defineProperties(obj1, {
    	fullName: {
    		//获取
    		get: function(){
    			return this.firstName + '-' + this.lastName;
    		},
    		//监听
    		set: function(data){
    			var nameArr = data.split("-");
    			this.firstName = nameArr[0];
    			this.lastName = nameArr[1];
    		}
    	}
    })
    console.log(obj1.fullName); //Kevin-Tseng
    obj1.fullName = "Tom-Smith";
    console.log(obj1.fullName, obj1.firstName); //Tom-Smith Tom
    

    Array扩展

    • indexOf()
    • lastIndexOf()
    • forEach(function(item, index){})
    • map(function(item, index){})
    • filter(function(item, index){})

    Function扩展

    • call() Function.call(obj, arguments) 将函数送给某对象,立即执行
    • apply() Function.apply(obj, [arguments]) 将函数送给某对象,立即执行
    • bind() Function.call(obj, arguments) 将函数送给某对象,调用执行
    var obj = {name: 'Kevin'};
    function foo(data){
    	console.log(this, data);
    }
    foo.call(obj, 33);//{name: "Kevin"} 33
    foo.apply(obj, [28]);//{name: "Kevin"} 28
    foo.bind(obj)(22); //{name: "Kevin"} 22
    
    setTimeout(function(data){
    	console.log(this, data); //{name: "Kevin"} 18
    }.bind(obj, 18),1000);
    
  • 相关阅读:
    PAT 00-自测1. 打印沙漏(20)
    js Ajax
    c语言算法实现
    解决python for vs在vs中无法使用中文
    python排序算法实现:
    2014-4-27 心情
    Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)
    Poj 1061 青蛙的约会(扩展欧几里得)
    hrbust 1328 相等的最小公倍数(数论)
    hdu 1286 找新朋友 (欧拉函数)
  • 原文地址:https://www.cnblogs.com/KevinTseng/p/11999972.html
Copyright © 2011-2022 走看看