How do I create a globally accessible variable?

前端 未结 3 502
你的背包
你的背包 2021-01-25 03:28

I\'m trying to make a variable thats can work anywhere, and just not in one function.

How do i do this? I\'ve searched for like 1 hour now and i can\'t find it :(

<
3条回答
  •  长情又很酷
    2021-01-25 03:39

    you can create a public function that has access to the private variables like this

    function Person() {
        var secret = "Secret Message";
    
        this.revealSecret = function() {
            return secret;
        }
    }
    var me = new Person();
    me.revealSecret(); //returns "Secret Message"
    

提交回复
热议问题