日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ECMAScript5 ES5

發(fā)布時(shí)間:2025/4/14 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ECMAScript5 ES5 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ECMAScript5新增一系列新的方法(API接口),新的瀏覽器中大部分是被支持的(IE9,Chrome,FirFor),有少量API不是所有瀏覽器都支 持

ES5通過(guò)對(duì)現(xiàn)有JavaScript方法添加語(yǔ)句和原生ECMAScript對(duì)象做合并實(shí)現(xiàn)標(biāo)準(zhǔn)化,IE9不支持嚴(yán)謹(jǐn)模式,但I(xiàn)E10是支持的。

?

Object.create(prototype, descriptors)

以指定的原型創(chuàng)建對(duì)象,并且可以(可選)的設(shè)置對(duì)象的屬性

function Poker(style, title, value) { this.Style = style; this.Title = title; this.Value = value; } var pokerA = Object.create(new Poker("club", "A", 14)); alert(Poker.constructor); //function Function() { [native code] } alert(new Poker().constructor); //function Poker(style, title, value) { // this.Style = style; // this.Title = title; // this.Value = value; // } alert(Poker.constructor.prototype); //function Empty() {} alert(Poker.prototype == new Poker().constructor.prototype); // true alert(Poker.constructor.prototype == new Poker().constructor.prototype); // false alert(new Poker().propertye); //undefined alert(Poker.prototype == pokerA.constructor.prototype); //true alert(Poker.constructor.prototype == pokerA.constructor.prototype); // false alert(new Poker("club", "A", 14).Value); //14 alert(pokerA.Value); //14

用Object.create構(gòu)造的對(duì)象和用構(gòu)造函數(shù)構(gòu)造的對(duì)象在結(jié)果上沒(méi)有什么差異。用 Object.create的好處是原型鏈干凈,網(wǎng)上有有給出了以下沒(méi)有Object.create的瀏覽器的解決同樣解決方案。以下代碼不但說(shuō)明了 Object.create的技術(shù)內(nèi)幕,同時(shí)可以支持低版本的IE同樣可以實(shí)現(xiàn)干凈的原型。

if (typeof Object.create !== 'function') { Object.create = function(o) { function F() { } F.prototype = o; return new F(); }; } function Poker(style, title, value) { this.Style = style; this.Title = title; this.Value = value; } var pokerA = Object.create(new Poker("club", "A", 14)); alert(Poker.constructor); //function Function() { [native code] } alert(new Poker().constructor); //function Poker(style, title, value) { // this.Style = style; // this.Title = title; // this.Value = value; // } alert(Poker.constructor.prototype); //function Empty() {} alert(Poker.prototype == new Poker().constructor.prototype); // true alert(Poker.constructor.prototype == new Poker().constructor.prototype); // false alert(new Poker().propertye); //undefined alert(Poker.prototype == pokerA.constructor.prototype); //true alert(Poker.constructor.prototype == pokerA.constructor.prototype); // false alert(new Poker("club", "A", 14).Value); //14 alert(pokerA.Value); //14

?

Object.defineProperty(object, propertyname, descriptor)

對(duì)指定的對(duì)象的一個(gè)屬性設(shè)置豐富的值控制


Object.defineProperties(object, descriptors)

對(duì)指定的對(duì)象的一組屬性提供豐富的值控制

function setPokerState(poker, proValue) { if (arguments[0] instanceof Poker) { Object.defineProperty(arguments[0], //為一個(gè)對(duì)象設(shè)置一個(gè)屬性 "State", //屬性名稱是字符串 {//一組修飾對(duì)象 value: proValue, //值 默認(rèn)是undefined writable: true, //值是否只讀 默認(rèn)是false enumerable: false, //值是否可以被枚舉 默認(rèn)false configurable: true//屬性是否可以被改說(shuō)刪除 默認(rèn)false } ) } } var PokerA = Object.create(new Poker("club", "A", 14)); setPokerState(PokerA, 5); alert(PokerA.State);

如果我們需要對(duì)賦值或取值有更多出來(lái),可以給定set和get函數(shù),不過(guò)set/get不能和value、writable同時(shí)使用。

function setPokerState(poker, proValue) { if (arguments[0] instanceof Poker) { Object.defineProperty(arguments[0], //為一個(gè)對(duì)象設(shè)置一個(gè)屬性 "State", //屬性名稱是字符串 {//一組修飾對(duì)象 enumerable: false, //值是否可以被枚舉 默認(rèn)false configurable: true, //屬性是否可以被改說(shuō)刪除 默認(rèn)false set: function(x) { this.state = x <= 5 ? x : 5; }, get: function() { return this.state; } } ) } } var PokerA = Object.create(new Poker("club", "A", 14)); PokerA.State=10;

如果我需要設(shè)置多個(gè)屬性的話,請(qǐng)看下面代碼演示

Object.defineProperties( PokerA, { "backgroundImg": { value: "images\\common\\hide.png", enumerable: false, //不可以for 遍歷 writable: false//只讀 }, "forgroundImg": { value: "images\\spade\\K.png", enumerable: false, //不可以for 遍歷 writable: false//只讀 }, Image: { get: function() { return this.State == 0 ? this.backgroundImg : this.forgroundImg; }, enumerable: true } } );

?

如果要了解對(duì)象有哪些屬性被定義了,可以使用以下API

Object.getOwnPropertyDescriptor(object, propertyname)
返回屬性的定義
Object.getOwnPropertyNames(object)
返回所有屬性的名稱,哪怕說(shuō)是不能枚舉的屬性

alert(Object.getOwnPropertyNames(PokerA).length); //4 var names = Object.getOwnPropertyNames(PokerA); for (var i = 0; i < names.length; i++) { alert(names[i] + "\n" + Object.getOwnPropertyDescriptor(PokerA, names[i]) ); }

?

可以約定對(duì)象不能再擴(kuò)展,但屬性的值可以改,也可以刪除

Object.preventExtensions(object)
防止新的屬性添加到對(duì)象
Object.isExtensible(object)
是否可添加屬性到對(duì)象

alert(Object.isExtensible(PokerA)); //true Object.preventExtensions(PokerA); alert(Object.isExtensible(PokerA)); //false

Object.seal(object)
不能添加和刪除屬性
Object.isSealed(object)

alert(Object.isSealed(PokerA)); //true Object.seal(PokerA); alert(Object.isSealed(PokerA)); //false

Object.freeze(object)
防止現(xiàn)有屬性和屬性值的修改,并防止新特性的添加。
Object.isFrozen(object)

?

最后如果想要得到對(duì)象原型,可以用

Object.getPrototypeOf(object)

?

關(guān)于object的就差不多上面這些了。然后看下ECMAScript5再其他對(duì)象上的擴(kuò)展的一些靜態(tài)成員

Array.isArray

alert(Array.isArray([])); //true alert(Array.isArray({})); //false

Array.IndexOf

Array.lastIndexOf

?

alert(["Hello", "javaScript", "ECMAScript", "HTML5"].indexOf("javaScript"));//1 alert(["Hello", "javaScript", "ECMAScript", "HTML5"].lastIndexOf("javaScript"));//1

Array.every

對(duì)數(shù)組的每個(gè)元素進(jìn)行一個(gè)callback的條件檢查,查看是否存在有不符合條件的元素。類似linq的all

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); alert(arr1.every( function(item, index) { return item.length < 5; } ) ); //false alert(arr1.every( function(item, index) { return item.length < 10; } ) )//true

Array.some

是判斷數(shù)組有沒(méi)有符合條件的元素,類似linq的any

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); alert(arr1.some( function(item, index) { return item.length < 5; } ) ); //true alert(arr1.some( function(item, index) { return item.length < 10; } ) )//true

如果需要對(duì)每一個(gè)元素執(zhí)行一段處理性的代碼可以用

Array.forEach

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); arr1.forEach( function(item, index) { if (index % 2 == 0) { arr1[index] = "name:" + item; } } ); arr1.forEach( function(item, index) { alert(item); } );

其實(shí)forEach最好是直接處理值,如果要改變數(shù)組最好用

Array.map

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); var arr2 = arr1.map( function(item, index) { if (item.indexOf("ll") > -1) { return item; } } ); arr2.forEach( function(item, index) { alert(item); } );

如果是要過(guò)濾數(shù)組的話,可以用

Array.filter

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); var arr2 = arr1.filter( function(item, index) { if (item.indexOf("ll") > -1) { return true; } } ); arr2.forEach( function(item, index) { alert(item); } );

如果要做疊加器處理可以用

Array.reduce

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); var arr2 = arr1.reduce( function(item, next) { return item + "][" + next; } ); alert("[" + arr2 + "]");//[Charles][Mark][Bill][Vincent][William][Joseph]

還有一個(gè)反向處理

Array.reduceRight

var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(","); var arr2 = arr1.reduceRight( function(item, next) { return item + "][" + next; } ); alert("[" + arr2 + "]"); //[Joseph][William][Vincent][Bill][Mark][Charles]

現(xiàn)在Array的處理都接近linq了!!!

?

Date().toJSON()

alert(new Date().toJSON()); alert(new Date(2012, 11, 12).toJSON()); //2012-12-11T16:00:00.000Z alert("[" + " Hello ".trim() + "]"); //[Hello]

?

轉(zhuǎn)載于:https://www.cnblogs.com/Watcher/p/3933545.html

總結(jié)

以上是生活随笔為你收集整理的ECMAScript5 ES5的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。