当前位置:网站首页>ES5 Object的扩展方法//call、apply 和 bind
ES5 Object的扩展方法//call、apply 和 bind
2022-06-22 21:24:00 【半夜删你代码·】
严格模式
ES5 除了正常运行模式(又称为混杂模式),还添加了第二种运行模式:"严格模式"(strict mode)。
严格模式顾名思义,就是使 JavaScript 在更严格的语法条件下运行。
作用
消除 JavaScript 语法的一些不合理、不严谨之处,减少一些怪异行为
消除代码运行的一些不安全之处,保证代码运行的安全
为未来新版本的 JavaScript 做好铺垫
使用
在全局或函数的第一条语句定义为:
'use strict'如果浏览器不支持,只解析为一条简单的语句, 没有任何副作用
// 全局使用严格模式
'use strict';
girl = '迪丽热巴';
// 函数中使用严格模式
function main(){
'use strict';
boy = '胡歌';
}
main();语法和行为改变
必须用 var 声明变量,不允许使用未声明的变量
禁止自定义的函数中的 this 指向 window
创建 eval 作用域
对象不能有重名的属性(Chrome 已经修复了这个 Bug,IE 还会出现)
函数不能有重复的形参
新增一些保留字, 如: implements interface private protected public
Object 扩展方法
Object.create(prototype, [descriptors])
Object.create 方法可以以指定对象为原型创建新的对象,同时可以为新的对象设置属性, 并对属性进行描述
value : 指定值
writable : 标识当前属性值是否是可修改的, 默认为 false
configurable:标识当前属性是否可以被删除 默认为 false
enumerable:标识当前属性是否能用for in 枚举 默认为 false
get: 当获取当前属性时的回调函数
set: 当设置当前属性时
//创建一个汽车的对象
var car = {
name : '汽车',
run: function(){
console.log('我可以行驶!!');
}
};
//以 car 为原型对象创建新对象
var aodi = Object.create(car, {
brand: {
value: '奥迪',
writable: false, //是否可修改
configurable: false, //是否可以删除
enumerable: true //是否可以使用 for...in 遍历
},
color: {
value : '黑色',
wriable: false,
configurable: false,
enumerable: true
}
});Object.defineProperties(object, descriptors)
直接在一个对象上定义新的属性或修改现有属性,并返回该对象。
object 要操作的对象
descriptors 属性描述
get 作为该属性的 getter 函数,如果没有 getter 则为undefined。函数返回值将被用作属性的值。
set 作为属性的 setter 函数,如果没有 setter 则为undefined。函数将仅接受参数赋值给该属性的新值。
// 定义对象
var star = {
firstName: '刘',
lastName : '德华'
};
// 为 star 定义额外的属性
Object.defineProperties(star, {
fullName: {
get: function(){
return this.firstName + this.lastName;
},
set: function(name){
var res = name.split('-');
this.firstName = res[0];
this.lastName = res[1];
}
}
});
// 修改 fullName 属性值
star.fullName = '张-学友';
// 打印属性
console.log(star.fullName);
call、apply 和 bind
call 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数
apply 方法调用一个具有给定 this 值的函数,以及作为一个数组(或类似数组对象)提供的参数
bind 同 call 相似,不过该方法会返回一个新的函数,而不会立即执行
function main(){
console.log(this);
}
/*1. 直接调用函数*/
main(); // window
/*2. 创建一个对象*/
var company = {name: '尚硅谷', age: 10};
/*使用这个对象调用 main 方法*/
main.call(company); // company
main.apply(company); // company
/*bind 修改 this 的值,返回一个新的函数*/
var fn = main.bind(company);
fn(); // company边栏推荐
- 2. interface (calculator)
- 为什么大家很少使用外键了?
- Spark SQL Generic Load/Save Functions(2.4.3)
- LeakCanary 源码详解(2)
- OJ每日一练——整理命名
- os. When the command line parameter in args[1:] is empty, the internal statement will not be executed
- 双重跨域:Access-Allow-Origin header contains multiple values“*, *”,but only one is allowed
- Asynchronous FIFO
- Use smart doc to automatically generate interface documents
- 保证数据库和缓存的一致性
猜你喜欢

OJ daily practice - spanning 2020

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

Customize multi-level list styles in word

Optimization - linear programming

After passing the hcip exam, I still failed to change my career. What do professional network workers value most

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

Redis缓存

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

10 Super VIM plug-ins, I can't put them down

SourceTree版本管理常用操作
随机推荐
阻止别人使用浏览器调试
node-fetch下载文件
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
uniapp 修改数组属性,视图不更新
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
xml转义字符对照表
Various schemes for lazy loading of pictures
SOA Service Oriented Architecture
Introduction to database access tools
Webrtc series - 4connection sorting of network transmission
Ensure database and cache consistency
OJ daily exercise - virus proliferation
Sword finger offer 05 Replace spaces
os. When the command line parameter in args[1:] is empty, the internal statement will not be executed
Considerations for using redisson to operate distributed queues
C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
OJ daily practice - sorting and naming
Problèmes rencontrés lors de l'utilisation de redistemplate
异步FIFO