开发者

Access inner function variables in Javascript

开发者 https://www.devze.com 2022-12-29 13:21 出处:网络
In many frameworks, internal function variables are used as private variables, for example Raphael = (function(){

In many frameworks, internal function variables are used as private variables, for example

Raphael = (function(){
    var _private = function(a,b) {return a+b;};
    var _public = function(a) {return _private(a,a);}
    var object = {mult2:_public};
    return object;
})();

here, we cannot access from the global namespace the variable named private, as it is an inner variable of the anonymous function in the first line.

Sometimes this function is contains a big Javascript framework, so that it wouldn开发者_如何学编程't pollute the global namespace.

I need to unit tests some object Raphael uses internally (in the above example, I wish to run unit tests on the object private). How can I test them?

edit: I received comments about unit tests which are supposed to test public interfaces.

Let me specify a use case. I'm writing a library called Raphael. This library is supposed to add only a single name to the global namespace, and nothing more. This is a peculiar requirement for Javascript, since Javascript does not have namespaces.

Let's say Raphael uses a linked list. If Javascript had the notion of packages, I would do

require 'linked_list'
Raphael = (function(){/* use linked list */})();

However Javascript does not allow me to do that in any way that wouldn't pollute the global scope with the linked list object! I'm therefore bound to inline linked_list into Raphael's local scope:

Raphael = (function(){
    /* implement linked list */
    var linked_list = function(){/*implementation*/};
})();

And now I want to test linked_list implementation.


You are still missing the point.

The point of unit testing is to verify that the object's public interface does what is expected of it. Unit tests show how the code works.

The only thing that should be tested is the object's public interface. That way, when you the developer want to change how the object is implemented, you only worry about if the object being tested still does what it is expected of it.

If you feel that the object that is inside that closure needs testing, then test that, but do it externally and then pass it into the closure.

var Raphael= function(listIterator) {
  listIterator.method();
}(new ListIterator());

Spurious hacks, such as shown below, are totally inappropriate (in unit tests or anywhere).

Test functions should be simple, test one thing only, and have one assertion. This can usually happen in three to ten lines of test code.

When you get to the point where your test functions are complicated, as they would be following the approach you are asking about, then either (1) realize that your design might not be what you want it to be and change it so that it is, or (2) change your expectations in the test.

Regarding the code you posted, you forgot var, missed a semicolon, and used two reserved words as identifiers: private and public.

The consequence of not using var is the potential to trigger errors and problems related to various implementations of nonstandard GlobalScopePolluter-type objects ("Object doesn't support this property or method" seen in IE). The consequence of using a FutureReservedWord is SyntaxError. Implementation may provide a syntax extesion to allow FutureReservedWord as identifier, and indeed many do, however it is best to not rely on such extensions and if you got an error, it would be completely your fault.

You mentioned delivering code to users. I suggest that you not do that until you get some more experience and understanding with what you are doing.

// DO NOT USE THIS CODE.
var Raphael = (function(){
    var _private = function(a,b) {return a+b;};
    var _public = function(a) {return _private(a,a);};
    var object = {mult2:_public};
    return object;
})();

var leakedFunction;

// Spurious hack:
//   Give valueOf a side effect of leaking function.
//   valueOf is called by the _private function as a
//   side effect of primitive conversion, where 
//   ToPrimitive(input argument, hint Number) results 
//   in calling valueOf.

function valueOfSnoop(){ 
    leakedFunction = leakedFunction || valueOfSnoop.caller || function(){};
    return 2;
}

var a = {
  valueOf : valueOfSnoop
};

Raphael.mult2(a, 3);
var privateMathod = leakedFunction;
alert(leakedFunction(1, 2));

That example code is only as a demonstration that such thing is possible. Given the choice, it is a poor alternative to the alternatives mentioned earlier; either change your design or change your tests.


Try this:

var adder = function(a,b) {
    return a + b;
}

Raphael = function(fn){
    var _private = function(a,b) {
        fn(a,b);
    }

    var _public = function(a) {
        return _private(a,a);
    }

    var object = {doubleIt: _public};

    return object;
}(adder);

Just a little function injection


The best solution I came up with:

In source Javascript file use

Raphael = (function(){
// start linked_list
    var linked_list = function() {/*...*/};
// end linked_list
    var object = {mult2:_public};
    return object;
})();

Now, use a script to extract objects between // start ([a-zA-Z_]*) and // end ([a-zA-Z_]*), and unit test the extracted code.

Apparently it is impossible to access variables in the inner scope of a function from an outer scope. As written in the SO question Jason linked to in the comments.


var Raphael;
var test = true; //or false;

Raphael = (function(){
    var private = function(a,b) {return a+b;};
    var public = function(a) {return private(a,a);}
    var object = {mult2:public};

    if (test) Raphael.private = private;

    return object;
})();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号