• 让js的forin循环禁止forin到某个属性的话要怎么做


    //知识点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______
  • 相关阅读:
    the Agiles Scrum Meeting 8
    the Agiles Scrum Meeting 7
    the Agiles Scrum Meeting 6
    项目使用说明——英文版
    第十次例会
    第九次例会
    第八次例会
    第六次例会
    第七次例会
    第五次例会
  • 原文地址:https://www.cnblogs.com/diligenceday/p/4057348.html
Copyright © 2020-2023  润新知