Vue不兼容IE8原因以及Object.defineProperty详解
Vue不兼容IE8原因以及Object.defineProperty详解
原因概述:
- Vue.js使用了IE8不能模拟的ECMAScript5特性. Vue.js支持所有兼容ES5的浏览器.
- Vue将遍历此对象所有的属性, 并使用Object.defineProperty把这些属性全部转为getter/setter.
- Object.defindProperty是仅ES5支持, 且无法shim的特性.
接下来逐步介绍概念.
shim特性
指把一个库引入一个旧的浏览器, 然后用旧的API, 实现一些新的API的功能.
Object.definePropety()
- 语法:
Object.definePropety(obj, prop, descriptor) - 参数:
- obj: 操作对象
- prop: 需要操作的属性名称
- descriptor: 属性具有的特性
- 返回值: 传入的对象, 即第一个参数obj
- 针对特性描述存在两种形式: 数据描述和存取器描述
数据描述
当修改或定义对象的时候, 给属性添加一些特性
var obj = {
test: 'hello'
}
// 对象已有的属相添加特性描述
Object.defineProperty(obj, 'test', {
configurable: true | false,
enumerable: true | false,
value: `任意类型的值`,
writable: true | false
})
// 对象新添加的属性描述
Object.defineProperty(obj, 'newKey', {
configurable: true | false,
enumerable: true | false,
value: `任意类型的值`,
writable: true | false
})
value
- 属性对应的值, 可以为任意类型的值.
- 默认:
undefined
// 不设置value的值
Object.defineProperty(obj, 'newKey', {
})
console.log(obj.newKey) // undefined
/*
注: 两段代码不能同时出现 ;
报错: Cannot redefine property: newKey
原因: configurable属性默认为false, 不能修改; writable默认fasle, 不能被重写
*/
// 设置value值
Object.defineProperty(obj, 'newKey', {
value: 'this is test'
})
console.log(obj.newKey) // undefined
writable
- 属性的是否可以被重写.
- 默认false, 不能被重写.
// writable为false, 不可被重写
Object.defineProperty(obj, 'newKey', {
value: 'hello',
writable: false
})
Object.defineProperty(obj, 'newKey', {
value: 'change'
})
// 这种情况下会报错: Cannot redefine property: newKey
console.log(obj.newKey)
// 可以被重写
Object.defineProperty(obj, 'newKey', {
value: 'hello',
writable: false
})
obj.newKey = 'change'
console.log(obj.newKey) // hello
enumerable
- 此属性是否可以枚举(使用for...in或者Object.keys)
- 默认为false: 不可枚举
// 不可枚举
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello'
})
console.dir(obj) // {}
// 可以枚举
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello',
enumerable: true
})
console.dir(obj) // { newKey: 'hello' }
configurable
- 目标属性是否可以被删除
- 目标属性的特性是否可以被再次修改
- 默认false, 不可删除与修改
// 属性不可被删除
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello',
configurable: false
})
delete obj.newKey
console.log(obj.newKey) // hello
// 属性可以被删除
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello',
configurable: true
})
delete obj.newKey
console.log(obj.newKey) // undefined
// 不能修改特性
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello',
writable: false,
configurable: false
})
Object.defineProperty(obj, 'newKey', {
writable: true,
})
// 报错: Cannot redefine property: newKey
// 再次修改特性
var obj = {}
Object.defineProperty(obj, 'newKey', {
value: 'hello',
writable: false,
configurable: true
})
Object.defineProperty(obj, 'newKey', {
writable: true,
})
obj.newKey = 'change'
console.log(obj.newKey) // change
注意
- 一旦使用Objec.defineProperty给对象添加属性, 如果不设置属性的话, 那么configuable, enumerable, writable这些都是默认的false
- 不能被枚举, 不能被重写, 不能被再次更改属性
存取器描述
当使用存取器描述特性的时候, 允许使用以下特性属性:
var obj = {}
Object.defineProperty(obj, 'newKey', {
get: function() {} | undefined,
set: function() {} | undefined,
configurable: true | false,
enumerable: true | false
})
- 当使用了getter或者setter方法, 不允许使用
writable和value这两个属性
getter/setter
- 当设置或获取某个对象的属性值的时候, 可以提供getter/setter方法
- getter: 是一种获取值的方法
- setter: 是一种设置值的方法
// 在特性中使用get/set属性来定义对应的方法
var obj = {}
var initVlue = 'hello'
Object.defineProperty(obj, 'newKey', {
get: function () {
// 当获取值的时候, 触发这个函数
return initVlue
},
set: function (value) {
// 设置值的时候, 触发这个函数
initVlue = value
}
})
// 获取值
console.log(obj.newKey) // hello
obj.newKey = 'change'
console.log(initVlue)// change
- get/set不必成对出现, 任写其一就可以. 如果设置不方便, 则get和set的默认值为undeifend
兼容性
在IE8下只能对DOM对象使用, 如果对原生对象使用Object.defineProtry()会报错
参考: https://segmentfault.com/a/1190000007434923
Vue不兼容IE8原因以及Object.defineProperty详解的更多相关文章
- IE8"开发人员工具"使用详解下(浏览器模式、文本模式、JavaScript调试、探查器)
来源: http://www.cnblogs.com/JustinYoung/archive/2009/04/03/kaifarenyuangongju2.html 在上一篇文章IE8“开发人员工具” ...
- IE8“开发人员工具”使用详解上(各级菜单详解)
来源: http://www.cnblogs.com/JustinYoung/archive/2009/03/24/kaifarenyuangongju.html IE8“开发人员工具”使用详解上(各 ...
- vue.js循环for(列表渲染)详解
vue.js循环for(列表渲染)详解 一.总结 一句话总结: v-for <ul id="example-1"> <li v-for="item in ...
- Vue通信、传值的多种方式,详解
Vue通信.传值的多种方式,详解 转自:https://blog.csdn.net/qq_35430000/article/details/79291287 一.通过路由带参数进行传值 ①两个组件 A ...
- vue.js选择if(条件渲染)详解
vue.js选择if(条件渲染)详解 一.总结 一句话总结: v-if <!DOCTYPE html> <html lang="en"> <head& ...
- 【转载】html中object标签详解
[转载自http://blog.csdn.net/soliy/archive/2010/03/22/5404183.aspx] html标签之Object标签详解 作者:网络 出处:网络 ...
- Vue通信、传值的多种方式,详解(都是干货)
Vue通信.传值的多种方式,详解(都是干货) 可参考博客: https://blog.csdn.net/qq_35430000/article/details/79291287
- JAVA中Object类方法详解
一.引言 Object是java所有类的基类,是整个类继承结构的顶端,也是最抽象的一个类.大家天天都在使用toString().equals().hashCode().waite().notify() ...
- Vue双向绑定的关键:Object.defineProperty()
这个方法了不起啊.vue.js和avalon.js 都是通过它实现双向绑定的.而且Object.observe也被草案发起人撤回了.所以defineProperty更有必要了解一下了. 先上几行代码看 ...
随机推荐
- RabbitMQ/pika模块
简介 MessageQueue用于解决跨进程.跨线程.跨应用.跨网络的通信问题. RabbitMQ使用erlang开发,在windows上使用时要先安装erlang. 官方的示例比较容易理解,可以点这 ...
- 编译异常之static和extern---more than one storage class specified
static 和 extern 不能同时共存 http://bbs.bccn.net/thread-58129-1-1.html
- 初识跨终端Web
近期试读了<跨终端Web>这本书的部分章节,既为了拿到书,也为了记录下读后的收获的东西,这会是个非常好的习惯吧. 标题为"初识跨终端Web".对我来说最贴切了,在此之前 ...
- day 18 面向对象的 继承
一.概念性知识 面向对象的三大特征: 继承,多态,封装 继承是创建新类的一种方法,在python中,新建的类可以继承一个或多个父类,父类称作基类或超类,新建的类又称作派生类或子类. 继承的好处 ...
- vmware nat不能上网的解决办法
1 很多奇怪的问题都是vmware突然不能上网导致的 当yum.pip等包管理工具突然不能上网了时,要ping www.baidu.com,看看网络是不是好的. 2 nat网络出现问题的解决办法 2. ...
- https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
- spring cloud 服务消费
Ribbon Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用. 当Ribbon与Eureka联合使用时,ribbonServerList会 ...
- (linux)自旋锁及其衍生锁
自旋锁 毫秒以下. 自旋锁用于多个CPU系统中,在单处理器系统中,自旋锁不起锁的作用,只是禁止或启用内核抢占.在自旋锁忙等待期间,内核抢占机制还是有效的,等待自旋锁释放的线程可能被更高优先级的线程 ...
- SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )
图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...
- Java经典算法大全
1.河内之塔.. 2.Algorithm Gossip: 费式数列. 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官(一) 6. ...