2017-02-11 91 views
2

我想创建自己的插件,但是,第一步遇到问题。我想要一个可以将参数作为默认参数的对象,并在其中具有其他函数。请看下面的例子,我正在努力完成。具有默认功能和其他功能的Javascript对象

var a = function(str) { console.info(str); } 
a = { 
    Test: function() { console.info(TestMessage()); }, 
    TestMessage: function() { return "Test Message"; } 
} 

基本上,我想要父对象,我可以自己调用一个参数。一个测试”);同时,我希望该父对象内的其他函数也可以访问该对象内的其他函数。 a.Test() - >调用a.TestMessage(),但不必编写“a”。每次都在这个物体里面。

回答

1

如果你想使用简单TestMessage函数引用保留,没有任何前缀,那么你可以创建一个闭包,在一个立即调用的函数表达式的格式。就像这样:

var a = (function() { // Immediately Invoked Function 
 
    // Define your functions here "at ease": 
 
    function testMessage() { 
 
     return "Test Message"; 
 
    } 
 
    function test() { 
 
     console.info(testMessage()); 
 
    } 
 
    function main(str) { 
 
     console.info(str); 
 
    } 
 
    // Now assign the sub-functions to main: 
 
    main.test = test; 
 
    main.testMessage = testMessage; 
 
    // And return that: 
 
    return main; 
 
})(); // execute immediately so to get the return value 
 

 
// Test it 
 

 
a('hello'); 
 
a.test();

3

与您的代码的问题是你覆盖a与第二个陈述。如果你想特性将其分配给属性添加到功能a指的是,你可以做到这一点就可以了:

var a = function(str) { console.info(str); }; 
a.Test = function() { console.info(TestMessage()); }; 
a.TestMessage = function() { return "Test Message"; }; 

现在,而不是在a与参考更换到一个新的对象的函数参考,我们只是将属性添加到它已经引用的函数对象中。

注意,虽然,内Test,你需要才能有资格TestMessage正确引用它:

a.Test = function() { console.info(a.TestMessage()); }; 
// --------------------------------^^ 

,或者如果你可以依靠a.Test总是被通过a调用,那么:

a.Test = function() { console.info(this.TestMessage()); }; 
// --------------------------------^^^^^ 

...但前者更可靠。

活生生的例子:

var a = function(str) { console.info(str); }; 
 
a.Test = function() { console.info(a.TestMessage()); }; 
 
a.TestMessage = function() { return "Test Message"; }; 
 

 
a("Direct"); 
 
a.Test();

+0

我知道了,谢谢。这是一个大插件的最佳方法吗?我在想像我会封装它,就像它会是一个大对象,里面会有所有可以调用对方的子功能,而不必写这个或父母的名字。我的意思是,如果这种方式对于一个大插件来说没问题的话,那么我对它就没问题。 – FerX32

+0

@AAA:听起来你真正想要的是“揭示模块模式”,在这里你使用了一个IIFE,它内部有私有函数,可以无限制地互相调用,并让它返回一个带有外部接口的对象。 –

+0

是的,这听起来有点像它(但是,有没有办法用这种模式做“默认”?像这样的:http://stackoverflow.com/questions/5647258/how-to-use-揭示模块模式在JavaScript中,也许我可以做些像揭露=功能(str),并显示(“直接”),它可以做“if(str!= undefined){console.info(str); return ;} ..你认为呢? – FerX32

0

您随时可以返回它可以有任意数量的功能的对象。一个函数使用this调用其对象的另一个函数。

我已经添加了一个非常简单的片段来解释这一点,请让我知道任何澄清。

var parent = createParent(); 
 

 
parent.callAllFunction(); 
 

 

 
function createParent() { 
 
return ParentConstructor(); 
 
} 
 

 
function ParentConstructor() { 
 
var obj = {}; 
 
    
 
obj.function1 = function1; 
 
obj.function2 = function2; 
 
obj.function3 = function3; 
 
obj.function4 = function4; 
 
obj.callAllFunction = callAllFunction; 
 
    
 
function function1() { 
 
    \t console.log('called function1'); 
 
} 
 

 
function function2() { 
 
    \t console.log('called function2'); 
 
} 
 

 
function function3() { 
 
    \t console.log('called function3'); 
 
} 
 

 
function function4() { 
 
    \t console.log('called function4'); 
 
} 
 

 
function callAllFunction() { 
 
\t this.function1(); 
 
\t this.function2(); 
 
\t this.function3(); 
 
    this.function4(); 
 
} 
 
    
 
return obj; 
 
}

而当你正在编写一个插件,你应该总是隔离主引用你的对象/模块,以使他们的可重用性和干净。

1

您还可以使用显示模块模式。

仅公开实际打算调用的函数。 辅助函数可以保留在对象内部,而不能从外部访问它们。

您还可以将初始str值保存在局部变量中,如var string = str;并用它在你的函数

function A (str) { 
    console.info(str); 

     function test() { 
      console.info(testMessage()); 
     } 

     function testMessage() { 
      return "Test Message"; 
     } 

    return { 
     test: test 
    } 
} 
var a = new A("testing"); 
a.test();