Javascript 变量中包含函数的变量
我有一个关于javascript的问题。为了使我的代码有条理,我将函数集中在一个变量中,如下所示Javascript 变量中包含函数的变量,javascript,Javascript,我有一个关于javascript的问题。为了使我的代码有条理,我将函数集中在一个变量中,如下所示 var helper = new function(){ this.writeProp = function(){ //write the property } this.getProp = function(){ //get property } this.updateProp = function(){ //get
var helper = new function(){
this.writeProp = function(){
//write the property
}
this.getProp = function(){
//get property
}
this.updateProp = function(){
//get property
}
}
到目前为止,代码仍在运行,但我可以使用helper.getProp()
现在函数的数量越来越大,所以我想调用像这样的函数helper.prop.get()
我怎样才能做到这一点
我的想法如下,但不起作用
var helper = new function(){
var prop = new function(){
this.write = function(){
//write the property
}
this.getProp = function(){
//get property
}
this.updateProp = function(){
//get property
}
}
}
正确的方法是什么?或者我不应该尝试以这种方式组织代码 JS中的每个函数都返回undefined,除非您显式返回其他内容,在这种情况下,您应该返回prop以保持链接 JS中的每个函数都返回undefined,除非您显式返回其他内容,在这种情况下,您应该返回prop以保持链接 只需将prop声明为函数的属性即可
var helper = new function(){
this.prop = new function(){
this.write = function(){
//write the property
}
this.getProp = function(){
//get property
}
this.updateProp = function(){
//get property
}
}
};
请尝试以下代码段:
var helper=新函数(){
this.prop=新函数(){
this.write=函数(){
//写入属性
}
this.getProp=函数(){
//取得财产
警报(“this.getProp”);
}
this.updateProp=函数(){
//取得财产
}
}
};
helper.prop.getProp()代码>您可以简单地将prop声明为函数的属性
var helper = new function(){
this.prop = new function(){
this.write = function(){
//write the property
}
this.getProp = function(){
//get property
}
this.updateProp = function(){
//get property
}
}
};
请尝试以下代码段:
var helper=新函数(){
this.prop=新函数(){
this.write=函数(){
//写入属性
}
this.getProp=函数(){
//取得财产
警报(“this.getProp”);
}
this.updateProp=函数(){
//取得财产
}
}
};
helper.prop.getProp()代码>像这样尝试
/*************************************************************/
// Modular approach to write javascript.
/*************************************************************/
var foo = (function () {
var publicAPI = {
bar: function () {
publicAPI.baz();
},
baz: function () {
console.log("Baz");
}
};
return publicAPI;
})();
foo.bar();
您可以遵循的另一种方法是这样的
var outerObj = {};
(function(test) {
'use strict';
test.sayHello = function(name) {
return 'Hi ' + name;
}
})(outerObj);
alert(outerObj.sayHello('Kaushik'));
像这样试试
/*************************************************************/
// Modular approach to write javascript.
/*************************************************************/
var foo = (function () {
var publicAPI = {
bar: function () {
publicAPI.baz();
},
baz: function () {
console.log("Baz");
}
};
return publicAPI;
})();
foo.bar();
您可以遵循的另一种方法是这样的
var outerObj = {};
(function(test) {
'use strict';
test.sayHello = function(name) {
return 'Hi ' + name;
}
})(outerObj);
alert(outerObj.sayHello('Kaushik'));
我不明白为什么helper
或helper.prop
应该是函数。为什么不是像这样的对象呢
var helper = {
prop: {
write: function() { ... },
...
}
};
我不明白为什么helper
或helper.prop
应该是函数。为什么不是像这样的对象呢
var helper = {
prop: {
write: function() { ... },
...
}
};
毕竟,返回prop
。您将在prop
中获得所有函数。由于prop
是函数中的私有变量,您无法从函数中访问私有变量。很抱歉,您可以发布一个代码示例。我无法从句子中理解它。谢谢:)@Slive2611类似这样的东西你使用新函数
而不是简单的{…}
有什么原因吗?如果你有一个单例对象,那么是的,绝对没有理由使用新函数
。请参见:.returnprop
。您将在prop
中获得所有函数。由于prop
是函数中的私有变量,您无法从函数中访问私有变量。很抱歉,您可以发布一个代码示例。我无法从句子中理解它。谢谢:)@Slive2611类似这样的东西你使用新函数
而不是简单的{…}
有什么原因吗?如果你有一个单例对象,那么是的,绝对没有理由使用新函数
。请参阅:。