简体   繁体   English

在javascript中访问“私有”变量

[英]Accessing “private” variables in javascript

Let's say I have the following class:假设我有以下课程:

function MyClass(){

    this.public = function(){
        console.log(private);
    };

    var private = 10;
}


var test = new MyClass;

test.public(); // logs 10
test.private; // undefined

I want to know if I can access the private variable from the outside.我想知道是否可以从外部访问私有变量。 Looking at chrome's console it seems to be possible, since its context is shown:查看 chrome 的控制台似乎是可能的,因为它显示了它的上下文:

在此处输入图片说明

EDIT: Just to clarify: I know how to properly expose it.编辑:只是为了澄清:我知道如何正确公开它。 Just want to be sure if there isn't some hacky way to access it.只是想确定是否没有一些hacky方式来访问它。

Your private variable is local to that function scope and within Javascript, there is no access to variables inside a scope from outside the scope.您的private变量是该函数作用域的局部变量,并且在 Javascript 中,无法从作用域外访问作用域内的变量。 You can ONLY access it from within the scope.您只能从范围内访问它。 There are no ways to get around this from Javascript code itself.没有办法从 Javascript 代码本身解决这个问题。

What a debugger can do (which has access to the VM internals) is different than what regular Javascript code can do.调试器可以做的事情(可以访问 VM 内部)与常规 Javascript 代码可以做的事情不同。 The debugger can look inside of scopes, but JS code from outside the scope cannot.调试器可以查看范围内,但范围外的 JS 代码不能。

Obviously you can make an accessor for it, but without an accessor, there is no way to get to it from the outside.显然你可以为它制作一个存取器,但是没有存取器,就没有办法从外面得到它。

The variable private is "trapped" inside MyClass 's closure.变量private被“困住”在MyClass的闭包中。 You cannot access it unless some code exposes it, like a "getter" function.除非某些代码公开它,否则您无法访问它,例如“getter”函数。

function MyClass(){

    this.getter= function(){
        return private;
    };

    var private = 10;
}

By the way, private is a reserved keyword .顺便说一下, private是一个保留关键字

There's one hacky, but not super-convenient, method.有一种笨拙但不是超级方便的方法。

Drop a breakpoint somewhere that has access to the scope.在可以访问范围的地方放置一个断点。

function MyClass(){

    this.public = function(){
        console.log(private); // <<< BREAKPOINT HERE!
    };

    var private = 10;
}

Now call the function that is public that'll get you to that breakpoint.现在调用公共函数,它将使您到达该断点。

While you're stuck there, pull private into global scope however you'd like by typing something like this on the console:当你被困在那里时,通过在控制台上输入这样的内容,将private拉入全局范围:

var window.foo = private;

Profit.利润。 You can now grab foo from the console whenever you'd like.您现在可以随时从控制台获取foo

And you can do this for anything available in scope there.您可以对范围内可用的任何内容执行此操作。 If you had var private2 = 7;如果你有var private2 = 7; in there under var private , you've got access to it, too, even if it's not used in this.public .var private ,您也可以访问它,即使它没有在this.public


Pros:优点:

  • Access the value any time while you're debugging.在调试时随时访问该值。
  • Congrats!恭喜! You're peak hipster.你是最时髦的人。

Cons:缺点:

  • Reload the page and you've gotta hack it again.重新加载页面,你必须再次破解它。
  • If you forget and put two foo s into global state, you'll overwrite the second.如果您忘记并将两个foo放入全局状态,您将覆盖第二个。

I keep figuring there's gotta be a sneakier (and more convenient) way, maybe like monkeypatching a public method back into itself with eval with the scope breaking code like the above (which would eliminate the breakpoint calling), but haven't quite found it yet.我一直认为必须有一种更狡猾(而且更方便)的方法,也许就像使用eval将公共方法修补回自身, eval使用上述范围破坏代码(这将消除断点调用),但还没有完全找到它还。

I have tried adding a getter function from outside but seemed not to work.我曾尝试从外部添加一个 getter 函数,但似乎不起作用。

something like this.像这样的东西。 If anyone has any idea about how to do this-like i would be really glad to read it.如果有人对如何做到这一点有任何想法,我会很高兴阅读它。

test('testing MyClass private var', () = {
  const priv = new MyClass()
  priv.getPrivate = function(){return this.private}
  
 expect(priv.getPrivate()).toBe(10)

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM