ecmaScript5新特性


瀏覽器支持狀況:
數組

  • Opera 11.60瀏覽器

  • Internet Explorer 9*安全

  • Firefox 4函數

  • Safari 5.1**this

  • Chrome 13spa

ES5的嚴格模式

嚴格模式給做者提供了選擇一個限制性更強語言變種的方式——給做者提供額外的可靠性給用戶提供額外的安全性。在JS文件或是函數的頂部添加"use strict"便可啓用嚴格模式。由於"use strict"就是個字符串,所以其會被舊版瀏覽器安全地忽視。prototype

"use strict";
function strict(){
  "use strict";  //...}function sloppy(){
  eval("window.foo = 'bar'");
}

附加對象

下面的方法是添加到Object上的構造器:code

  • Object.getPrototypeOform

  • Object.getOwnPropertyDescriptor對象

  • Object.getOwnPropertyNames

  • Object.create

  • Object.defineProperty

  • Object.defineProperties

  • Object.seal

  • Object.freeze

  • Object.preventExtensions

  • Object.isSealed

  • Object.isFrozen

  • Object.isExtensible

  • Object.keys

"use strict";

function Animal () {
   this.name = "animal";
   this.age = 0;
}

Animal.prototype.say = function () {
   alert(this.name);
}

var dog = Object.create(new Animal(), {
   run: {
      value: true,
      writable: true,
      enumerable: true,
      configurable: true
   }
}) //建立對象

Object.defineProperty(dog, "run", {
   enumerable: false
}) //定義單個屬性

Object.defineProperties(dog, {
   hair: {
      value: "black",
      writable: false,
      enumerable: true,
      configurable: true
   },
   nose: {
      set: function (value) {
         this.noseValue = value;
      },
      get: function () {
         return "dog's nose" + this.noseValue;
      },
      enumerable: true,
      configurable: true
   }
}) //定義多個屬性

for (var i in dog) {
   console.log(i);
}

console.log(Object.getOwnPropertyDescriptor(dog, "run")); //獲取某個屬性的特性描述(value, enumerable, writable, configurable)
console.log(Object.getPrototypeOf(dog)); //獲取原型對象
console.log(Object.getOwnPropertyNames(dog)); //獲取自身屬性名列表,名包括enumerable爲false的屬性
dog.nose = "靈敏";
console.log(dog.nose);
console.log(Object.keys(dog)); //取自身屬性名列表,不包括enumerable爲false的屬性
Object.preventExtensions(dog); //阻止向對象添加屬性
Object.seal(dog);  //阻止修改現有屬性的特性,阻止繼續添加新屬性
Object.freeze(dog);  //阻止修改現有屬性的特性和值,阻止繼續添加新屬性
console.log(Object.isExtensible(dog));
console.log(Object.isSealed(dog));
console.log(Object.isFrozen(dog));
Object.defineProperty(dog, "hair", {
   writable: true
})
dog.hair = 22;
console.log(dog.hair)

Function.prototype.bind(thisArg [, arg1 [, arg2, …]])

Function.prototype.bind返回一個新的函數對象,該函數對象的this綁定到了thisArg參數上。從本質上講,這容許你在其餘對象鏈中執行一個函數。

"use strict";

function run (type) {
   console.log(this.speed);
   console.log(type);
}

var obj = {
   speed: "50km/h"
}

var runBind = run.bind(obj, "average");

runBind();

額外的數組

如下方法添加到了Arrayprototype對象上:

  • Array.prototype.indexOf

  • Array.prototype.lastIndexOf

  • Array.prototype.every

  • Array.prototype.some

  • Array.prototype.forEach

  • Array.prototype.map

  • Array.prototype.filter

  • Array.prototype.reduce

  • Array.prototype.reduceRight

"use strict";

var arr = [1,3,2,6,6,8];

console.log(Array.isArray(arr)); //判斷是不是一個數組
console.log(arr.indexOf(6)); //判斷某個值在數組中第一次出現的位置
console.log(arr.lastIndexOf(6)); //判斷某個值在數組中最後一次出現的位置

var everyTrue = arr.every(function (value) { //數組中每一個元素都知足條件,返回true,有一個不知足返回false
   return value > 0;
})
console.log(everyTrue);

var someTrue = arr.some(function (value) { //數組中至少有一個元素知足,返回true,都不知足返回false
   return value > 5;
})
console.log(someTrue);

arr.forEach(function (value, index) { //遍歷數組
   console.log(index, value)
})

var arr1 = arr.map(function (value) { //組裝新數組,原數組不變
   return value + 10;
})
console.log(arr1);

var arr2 = arr.filter(function (value) { //組裝新數組,過濾掉不知足條件的元素,原數組不變
   return value > 3
})
console.log(arr2)

var total = arr.reduce(function (total, value) { //對每一個元素進行累積
   return total + value;
}, 10)
console.log(total);

var total = arr.reduceRight(function (total, value) { //對每一個元素進行反向累積
   return total + value;
}, 10)
console.log(total);
相關文章
相關標籤/搜索