为什么 "This" 在这种情况下充当 javascript 函数中的私有成员?

Why "This" does act as a private member in a javascript function in this case?

我尝试在一个函数中调用一个 public 成员,它显示为 'undefined' 但是该函数有效...

-- 编辑--
正如@Shane Voisard 所指出的,this 是我函数父级的成员。

解决方案是为函数命名(如 Shane 在回答中下划线所示)。

... 或将函数附加到名称:

MySite.Public.Logo = { 

        inject : function(target){

             this.name = 'Logo';
             ...
        }
}

在控制台中:

MySite.Public.Modules.Logo.name; //return "Logo" 

等等...谢谢所以

-- /编辑--

我的文件系统是这样的

root folder :
-rw-r--r--  1 www-data www-data 1921 avril  3 17:31 0.js
drwxr-xr-x  9 www-data www-data 4096 avril  3 17:37 public

public folder :
-rw-r--r-- 1 www-data www-data  214 avril  3 17:37 0.js
drwxr-xr-x 2 www-data www-data 4096 avril  3 17:40 modules
drwxr-xr-x 2 www-data www-data 4096 avril  3 17:40 factories
drwxr-xr-x 2 www-data www-data 4096 avril  3 17:40 functions
drwxr-xr-x 2 www-data www-data 4096 avril  2 22:22 singleton

module folder : 
-rw-r--r-- 1 www-data www-data  28 mars  31 06:07 1.js
-rw-r--r-- 1 www-data www-data 348 avril  3 17:51 logo.js

按以下顺序加载:
root/0.js 初始化 MySite = {}
public/0.js 初始化 MySite.Public = {}
module/1.js 初始化 MySite.Public.Modules = {}

当加载所有其他需要的文件时,public/0.js 调用 MySite.Public.Modules.Logo(),它已在 logo.js 加载到浏览器时初始化。该函数有效,我可以从 logo.js 得到一个 return。但是 this 成员中的 none 是 public 容易访问的...

logo.js 的内容:

MySite.Public.Modules.Logo = function(){

        this.name = 'Logo';

        this.build = function(){
                var logo = MySite.Public.Factories.HTML.make('logo');
                return logo;
        };  

        this.inject = function(target){
                var logo = this.build();
                var target = MySite.Public.Functions.ToolBox.getElementByName(target); //getElementByName is a custom function
                target.appendChild(logo);
        }; 

}

在控制台中:

MySite.Public.Modules.Logo.inject('body'); // undefined is not a function...
MySite.Public.Modules.Logo.build(); // undefined is not a function...
MySite.Public.Modules.Logo.name; // ""
typeof MySite.Public.Modules.Logo; // "function"

logo.js的内容://测试我给构造函数目标的函数

MySite.Public.Modules.Logo = function(target){

        this.name = 'Logo';

        this.build = function(){
                var logo = MySite.Public.Factories.HTML.make('logo');
                return logo;
        };  

        this.inject = function(target){
                var logo = this.build();
                var target = MySite.Public.Functions.ToolBox.getElementByName(target);
                target.appendChild(logo);
        };  

        this.inject(target);

}

在控制台中:

MySite.Public.Modules.Logo('body'); //works

而且还是...

MySite.Public.Modules.Logo.inject('body'); // undefined is not a function...
MySite.Public.Modules.Logo.build(); // undefined is not a function...
MySite.Public.Modules.Logo.name; // ""
typeof MySite.Public.Modules.Logo; // "function"

有人知道我做错了什么吗?

尝试:

MySite.Public.Modules.Logo()
MySite.Public.Modules.name // "Logo"

当您调用 Logo() 时,this.name 引用 MySite.Public.Modules 对象。当您在对象方法上下文中调用函数时,javascript 在运行时将对象隐式分配给调用函数内的 this

如果你想要在你的函数对象上定义的方法,给函数一个名字,然后在函数内部引用这个名字:

MySite.Public.Modules.Logo = function init(target){

        init.l_name = 'Logo';
        init.getName = function { return l_name };

        init.build = function(){
                var logo = MySite.Public.Factories.HTML.make('logo');
                return logo;
        };  

        init.inject = function(target){
                var logo = init.build();
                var target = MySite.Public.Functions.ToolBox.getElementByName(target);
                target.appendChild(logo);
        };  

        init.inject(target);

}

编辑name property of a function object might be read only,取决于javascript环境。因此,您可能需要在内部使用不同的 属性 名称并公开漂亮的访问器方法。例如,在 Chrome 开发者工具中,我无法分配给 init.name.

MySite 是您的全局对象,但您引用 Innov24 就好像 Innov24 = MySite.

我强烈推荐Javascript: The Good Parts by Douglas Crockford