讓js的forin迴圈禁止forin到某個屬性的話要怎麼做

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   使用   for   sp   div   

//知識點1:for In迴圈是可以枚舉到繼承的屬性的;
//知識點2:使用defineProperty讓屬性無法通過forIn枚舉到;
//知識點3:用definedProperty重新定義一個屬性藥把這個屬性設定為空白,然後重新定義才行哦;
var obj = {}; Object.defineProperty(obj, "name", { get : function() {return 1}, enumberable : false }); //使用ECMA5中的defineProperty可以讓自訂屬性不可枚舉; for( prop in obj ){ console.log(prop) //name是無法枚舉到的; }; function extend( _super ) { var F = function() {}; F.prototype = _super; return (new F); }; var Fn = function(){}; Fn.prototype = { constructor : Fn, __prop______ : "super" }; var subInstance = extend( new Fn() ); //for in 迴圈是可以枚舉到父級的繼承屬性的; for(var prop in subInstance) console.log(prop)//constructor ,__prop______ //要讓繼承的屬性也無法枚舉到,那就在父級中設定enumberable; Object.defineProperty(Fn.prototype, "_test",{ enumberable : false , value : "__new_super" }); //PS,如果你重新定義__prop____為不可以枚舉不可行,必須要把__prop____設定為空白然後重新定義不可以枚舉的屬性才行; Fn.prototype.__prop______ = undefined; Object.defineProperty(Fn.prototype, "__prop______",{ enumberable : false , value : "_new___prop______" }); var subInstance = extend( new Fn() ); for(var prop in subInstance); console.log(prop)//constructor ,__prop______

 

讓js的forin迴圈禁止forin到某個屬性的話要怎麼做

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.