简体   繁体   English

JAVASCRIPT:我如何从外部访问这些 javascript 成员函数?

[英]JAVASCRIPT: How do i access these javascript member functions from outside?

I know how to access the below member function when it's written like this:我知道如何访问下面的成员 function 当它这样写时:

var blady_blah=
{
    some_member_function: function ()
    {
    }

}

I access it from outside doing blady_blah.some_member_function()我从外部访问它blady_blah.some_member_function()

But how do I access the member function when it's written like this:但是当它这样写时,我如何访问成员 function:

(function() {

    some_member_function: function ()
    {
    }

})();

Braces, { }, are used to define both object literals and function bodies.大括号 { } 用于定义 object 文字和 function 主体。 The difference is:区别在于:

var name = {};    // Object literal

Which you may also see written as您可能还会看到写成

var name = {

};

That's just the same but with some space in between so it's still an object literal, and unfortunately it looks very similar to:那是一样的,但中间有一些空格,所以它仍然是 object 文字,不幸的是它看起来非常类似于:

var name = function () {    // Function body

};

An object can have members: object 可以有成员:

var name = {
  member: "string"
};

Whereas a function cannot;而 function 不能; a function has statements: function 有声明:

var name = function () {
  do_something();
  var result = do_something_else();
};

You can't write你不能写

var name = function () {
  member: "string"
};

Because you've mixed the two uses of { } together.因为您将 { } 的两种用法混合在一起。

A variable can be defined within a function, but it can't be seen outside the function - it's within the function scope:可以在ZC1C425268E68385D1AB5074C174F14Z中定义变量,但是在ZC1C42525268E683851AB55074C1717A94F14BES之外看不到它,它在ZC1C11C425268EMEN中。

var name = function () {
  var something_useful = string;
};

The second example is a closure (it just happens to have a syntax error inside).第二个例子是一个闭包(它只是碰巧在里面有一个语法错误)。 Minus the bad syntax, your self-evaluating anonymous function looks like this:减去不好的语法,您的自我评估匿名 function 看起来像这样:

(function() {
})();

If you'd like, you can define functions inside this that will be invisible to the outside world.如果您愿意,您可以在其中定义外部世界不可见的函数。 This is useful if you're interested in maintaining a clean global namespace, for example with library code.如果您对维护一个干净的全局命名空间感兴趣,例如使用库代码,这将非常有用。

(function() {
  function utilityFunctionFoo() {
  }

  function utilityFunctionBar() {
  }
})();

Of course, if you'd like to call any of these functions from the outside world, you're out of luck.当然,如果你想从外界调用这些函数中的任何一个,那你就不走运了。 Or are you?还是你? Actually, there's another way to define a function:实际上,还有另一种定义 function 的方法:

var foo = function() {
}

That's exactly the same as writing:这与写作完全相同:

function foo() {
}

...Except that when written in the second style, you can actually omit the var keyword and create a global variable: Bringing it all together: ...除了以第二种样式编写时,您实际上可以省略var关键字并创建一个全局变量:将它们放在一起:

(function() {
  publicData = "stuff accessible from outside anonymous function";
  var privateData = "stuff that stays inside anonymous function";

  function utilityFunctionFoo() {
  }

  function utilityFunctionBar() {
  }


  usefulFunctionExport = function() {
    utilityFunctionFoo();
    utilityFunctionBar();
  }
})();

usefulFunctionExport();

You can't access it after the function it's in terminates.在 function 终止后,您将无法访问它。 It's a local variable that goes out of scope when its parent function ends.它是一个局部变量,当它的父 function 结束时,它会从 scope 结束。

You should make the main function be a constructor so that it returns a new instance of a class (you could name it Blahdy_blah) with the member function as one of its properties.您应该使主 function 成为构造函数,以便它返回 class 的新实例(您可以将其命名为 Blahdy_blah),其中成员 ZC1C425268E68385D1AB5074C17A94F14 是其属性之一。

Look up constructors, their return values, and accessing member variables.查找构造函数、它们的返回值和访问成员变量。

If you want to execute the function you need to return an object that exposes the function.如果你想执行 function,你需要返回一个 object,它会暴露 function。

var LIB = (function() {

    var fn = {
        member_function : function(){}
    };

    return fn;


})();

and to call并打电话

LIB.member_function();
(function() {

    blady_blah.some_member_function();

})();

If you need to add stuff into it you would write it like this.如果你需要往里面添加东西,你可以这样写。

(function() {

    blady_blah.some_member_function(function(){

        // Do stuff...

    });

})();

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

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