公开闭包内的方法

发布于 2024-10-07 02:29:14 字数 73 浏览 1 评论 0原文

当我们在闭包中创建一个方法时,它就成为该闭包的私有方法,并且在我们以某种方式公开它之前无法访问它。

怎么可能暴露呢?

When we are creating a method inside a closure it becomes private to that closure and can't be accessed until we expose it in some way.

How can it be exposed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御弟哥哥 2024-10-14 02:29:14

您可以返回对它的引用...

var a = function() {

   var b = function() {
      // I'm private!
      alert('go away!');
   };

   return {
      b: b // Not anymore!
   };

};

在 jsFiddle 上查看

您还可以将其绑定到window对象。但我更喜欢上面的方法,否则您将通过全局变量(作为 window 对象的属性)公开它。

You can return a reference to it...

var a = function() {

   var b = function() {
      // I'm private!
      alert('go away!');
   };

   return {
      b: b // Not anymore!
   };

};

See it on jsFiddle.

You could also bind it to the window object. But I prefer the method above, otherwise you are exposing it via a global variable (being a property of the window object).

秋心╮凉 2024-10-14 02:29:14

您需要以某种方式将其传递到外部。

示例: http://jsfiddle.net/patrick_dw/T9vnn/1/< /a>

function someFunc() {

    var privateFunc = function() {
        alert('expose me!');
    }

    // Method 1: Directly assign it to an outer scope
    window.exposed = privateFunc;

    // Method 2: pass it out as a function argument
    someOuterFunction( privateFunc );

    // Method 3: return it
    return privateFunc;
}

someFunc()(); // alerts "expose me!"

function someOuterFunction( fn ) {
    fn(); // alerts "expose me!"
}

window.exposed(); // alerts "expose me!"

You need to pass it to the outside in some manner.

Example: http://jsfiddle.net/patrick_dw/T9vnn/1/

function someFunc() {

    var privateFunc = function() {
        alert('expose me!');
    }

    // Method 1: Directly assign it to an outer scope
    window.exposed = privateFunc;

    // Method 2: pass it out as a function argument
    someOuterFunction( privateFunc );

    // Method 3: return it
    return privateFunc;
}

someFunc()(); // alerts "expose me!"

function someOuterFunction( fn ) {
    fn(); // alerts "expose me!"
}

window.exposed(); // alerts "expose me!"
满栀 2024-10-14 02:29:14

您可以通过在 this 作用域(可以根据调用而改变)中内部声明闭包的函数或属性来公开它们。

function example(val) {

    var value = val;

    this.getVal = function() {
        return value;
    }

    this.setVal = function(v) {
        value = v;
    }
}

var ex = new example(2);
ex.getVal();  // == 2
ex.setVal(4); // == null
ex.getVal();  // == 4

this 中声明的方法可以访问使用 var 声明的变量,但反之则不行。

function example(val) {

    var value = val;

    var double = function(v) {
        return 2 * v;
    }

    this.getDouble = function() {
        return double(value);
    }
}


var ex = new example(2);
ex.getDouble(); // == 4

函数在范围内关闭。您想要做的是返回对有权访问所需范围的函数的引用,以便您可以在以后调用它。

如果您需要创建一个在稍后调用特定方法的函数,

var ex = new example(2);
var delayed_call = function() {
    return(ex.getDouble()); // == 4, when called
}
setTimeout(delayed_call, 1000);

如果作用域是一个问题,

var ex = new example(2);
var delayed_call = (function(ex_ref) {
    return function() {
        return(ex_ref.getDouble()); // == 4, when called
    }
})(ex); // create a new scope and capture a reference to ex as ex_ref
setTimeout(delayed_call, 1000);

您可以使用不太可读的示例内联大部分内容,

setTimeout((function(ex_ref) {
    return function() {
        return(ex_ref.getDouble()); // == 4, when called
    })(new example(2))) 
    , 1000
);

setTimeout 只是一种方便的方法展示新范围内的执行力。

var ex = new example(2);
var delayed_call = function() {
    return(ex.getDouble());
}
delayed_call(); // == 4

You expose functions or properties of a closure by internally declaring them in this scope (which can change depending on invocation).

function example(val) {

    var value = val;

    this.getVal = function() {
        return value;
    }

    this.setVal = function(v) {
        value = v;
    }
}

var ex = new example(2);
ex.getVal();  // == 2
ex.setVal(4); // == null
ex.getVal();  // == 4

Methods declared in this can access variables declared using var, but not the other way 'round.

function example(val) {

    var value = val;

    var double = function(v) {
        return 2 * v;
    }

    this.getDouble = function() {
        return double(value);
    }
}


var ex = new example(2);
ex.getDouble(); // == 4

The function closes over the scope. What you want to do is to return a reference to a function that has access to the scope you require so you can invoke it at a later point.

If you need to create a function that calls a specific method at some later point,

var ex = new example(2);
var delayed_call = function() {
    return(ex.getDouble()); // == 4, when called
}
setTimeout(delayed_call, 1000);

If scoping is an issue,

var ex = new example(2);
var delayed_call = (function(ex_ref) {
    return function() {
        return(ex_ref.getDouble()); // == 4, when called
    }
})(ex); // create a new scope and capture a reference to ex as ex_ref
setTimeout(delayed_call, 1000);

You can inline most of this with the less readable example of,

setTimeout((function(ex_ref) {
    return function() {
        return(ex_ref.getDouble()); // == 4, when called
    })(new example(2))) 
    , 1000
);

setTimeout is just a convenient way of demonstrating execution in new scope.

var ex = new example(2);
var delayed_call = function() {
    return(ex.getDouble());
}
delayed_call(); // == 4
捎一片雪花 2024-10-14 02:29:14

出于性能目的,您可以通过以下方式调用它:

var a = (function(){
   function _a(){}
   _a.prototype = (function(){
     var _test = function(){ console.log("test"); };
     return {
       test: _test
     }
   }());

  return new _a();
}());

// usage
var x = a;
x.test(); // "test"

For performance purposes you can invoke it this way:

var a = (function(){
   function _a(){}
   _a.prototype = (function(){
     var _test = function(){ console.log("test"); };
     return {
       test: _test
     }
   }());

  return new _a();
}());

// usage
var x = a;
x.test(); // "test"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文