ES5新增的东西数组
2、对象方法安全
一、Object.getPrototypeOf(object)函数
返回对象的原型ui
function Pasta(grain, width) { this.grain = grain; this.width = width; } var spaghetti = new Pasta("wheat", 0.2); var proto = Object.getPrototypeOf(spaghetti); console.log(proto)
输出:this
二、Object.createspa
必需。 要用做原型的对象。 能够为 null。 prototype
descriptors可选。 包含一个或多个属性描述符的 JavaScript 对象。 3d
“数据属性”是可获取且可设置值的属性。 code
数据属性描述符包含 value 特性,对象
以及 writable、enumerable 和 configurable 特性。
若是未指定最后三个特性,则它们默认为 false。
返回:一个具备指定的内部原型且包含指定的属性(若是有)的新对象
var newObj = Object.create(null, { size: { value: "large", enumerable: true }, shape: { value: "round", enumerable: true } }); document.write(newObj.size + "<br/>"); document.write(newObj.shape + "<br/>"); document.write(Object.getPrototypeOf(newObj)); // Output: // large // round // null
Object.defineProperty(obj, prop, descriptor)
Object.defineProperties(obj, props)
obj: 将要被添加属性或修改属性的对象
var obj = new Object(); Object.defineProperties(obj, { name: { value: '张三', configurable: false, writable: true, enumerable: true }, age: { value: 18, configurable: true } }) console.log(obj.name, obj.age) // 张三, 18
var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.keys(obj)); // console: ['0', '1', '2']
3、 strict模式(严格模式)
“use strict”
将"use strict"放在脚本文件的第一行,则整个脚本都将以"严格模式"运行。
严格模式影响范围
设立"严格模式"的目的,主要有如下几个:错误检测、规范、效率、安全、面向将来
- 消除Javascript语法的一些不合理、不严谨之处,减小一些怪异行为;
- 消除代码运行的一些不安全之处,保证代码运行的安全;
- 提升编译器效率,增长运行速度;
- 为将来新版本的Javascript作好铺垫。
将"use strict"放在函数体的第一行,则整个函数以"严格模式"运行。
function strict(){ "use strict"; return "这是严格模式。"; } function notStrict() { return "这是正常模式。"; }
具体体现:
a、变量都必须先用var命令显示声明,而后再使用
b、严格模式不能对变量调用 delete 操做符
c、不用保留字作 变量名 或 参数名
d、为只读属性赋值报错
e、严格模式下参数名不能重复
f、只能在脚本的顶级和在函数内部申明函数,if for等语句中申明函数会致使语法错误
g、严格模式下抑制this
4、Array.isArray()
用于肯定传递的值是不是一个 Array
。
Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); // false Array.isArray("foobar"); // false Array.isArray(undefined); // false
// 下面的函数调用都返回 true Array.isArray([]); Array.isArray([1]); Array.isArray(new Array()); // 不为人知的事实:其实 Array.prototype 也是一个数组。 Array.isArray(Array.prototype); // 下面的函数调用都返回 false Array.isArray(); Array.isArray({}); Array.isArray(null); Array.isArray(undefined); Array.isArray(17); Array.isArray('Array'); Array.isArray(true); Array.isArray(false); Array.isArray({ __proto__: Array.prototype });
instanceof
和 isArray
当检测Array实例时, Array.isArray
优于 instanceof,由于Array.isArray能检测iframes
.