Is it possible to create a hidden property in javascript

前端 未结 6 2047
刺人心
刺人心 2020-12-08 04:59

I want to create an object with a hidden property(a property that does not show up in a for (var x in obj loop). Is it possible to do this?

6条回答
  •  执笔经年
    2020-12-08 05:43

    var Foo=function(s){
        var hidden
        this.setName=function(name){theName=name}
        this.toString=function(){return theName}
        this.public=s
    }
    var X=new Foo('The X')
    X.setName('This is X')
    X // returns 'This is X'
    X.public // returns 'The X'
    X.hidden // returns 'undefined'
    

提交回复
热议问题