博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES5扩展属性
阅读量:4594 次
发布时间:2019-06-09

本文共 2622 字,大约阅读时间需要 8 分钟。

一: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(){} 用来监视当前属性值变化的回调函数

  

二:数组的扩展属性

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()是将函数返回

  

 

转载于:https://www.cnblogs.com/love-life-insist/p/9909298.html

你可能感兴趣的文章
跨库事务一致性问题的解决方式(例)
查看>>
ios build时,Undefined symbols for architecture xxx问题的总结
查看>>
JavaScript对象
查看>>
南理第八届校赛同步赛-C count_prime//容斥原理
查看>>
CentOS7.4下使用Nginx配置Asp.net Core和添加Https证书步骤
查看>>
常用模块介绍
查看>>
一台云服务器怎么同时响应多个域名?
查看>>
【黑客免杀攻防】读书笔记1 - 初级免杀基础理论(反病毒软件特征码提取介绍、免杀原理、壳)...
查看>>
Java 枚举类
查看>>
noip模拟赛 PA
查看>>
Codeforces 717.F Heroes of Making Magic III
查看>>
noip2011 选择客栈
查看>>
poj1161
查看>>
nginx报错,需要zlib和pcre
查看>>
ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
查看>>
ACM程序设计选修课——1030: Hungar的时尚球场(水题+耐心)
查看>>
NBOJv2 1034 Salary Inequity(DFS序+线段树区间更新区间(最值)查询)
查看>>
Python学习笔记之抽象
查看>>
ts, vconsle显示‘Unexpected strict mode reserved word’
查看>>
聊聊我这两年都在忙什么,IT技术男如何转型!
查看>>