Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递参数的Javascript类_Javascript - Fatal编程技术网

传递参数的Javascript类

传递参数的Javascript类,javascript,Javascript,我已经创建了几个类,但是我从来没有在类本身上有任何需要的参数 下面的代码工作得很好 $(function() { search.anotherFunction('a', 'b'); }); search = function() { this.anotherFunction = function(param1, param2) { // do whatever }; var public = { anotherFunction: anotherFu

我已经创建了几个类,但是我从来没有在类本身上有任何需要的参数

下面的代码工作得很好

$(function()
{
   search.anotherFunction('a', 'b');
});

search = function()
{

   this.anotherFunction = function(param1, param2)
   {
      // do whatever
   };

   var public = { anotherFunction: anotherFunction }  

   return public;
}();
但是现在我想在
search
中传递参数,以避免将相同的参数传递给所有函数

$(function()
{
   search('front/file.php').anotherFunction('a', 'b');
});

search = function(url)
{

   this.anotherFunction = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   this.anotherFunctionB = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var public = { anotherFunction: anotherFunction,
                  anotherFunctionB: anotherFunctionB }  

   return public;
}();
这不起作用,控制台输出错误

未捕获类型错误:对象不是函数


这意味着
search
不是一个函数,而是一个类名,因此无法接收参数?

首先,您创建“类”的方式是不正确的,并最终创建了全局变量:在匿名函数的调用中,由于您调用它的方式,
this
将引用全局对象*,因此
this.anotherFunction=…
将创建一个名为
anotherFunction
的全局变量,因为全局对象上的属性是全局变量

如果您想继续使用当前模式进行最小的更改,则不要在函数中使用
this.xyz=…
,而是使用
var

var search = function()
{
   var anotherFunction = function(param1, param2)
   {
      // do whatever
   };

   var public = { anotherFunction: anotherFunction }  

   return public;
}();
还请注意,您没有声明
search
,这是您的牺牲品;我添加了一个
var
来声明它

如果您没有调用最外层的函数,只是将该函数分配给
search
变量,然后稍后调用它,那么您的第二个示例(具有上述更改)将起作用:

var search = function(url)
{

   var anotherFunction = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var anotherFunctionB = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var public = { anotherFunction: anotherFunction,
                  anotherFunctionB: anotherFunctionB }  

   return public;
}; // <== Note, no () here

*为什么调用匿名函数时,
引用全局对象?因为你调用它时没有做任何事情来设置
这个
,而是使用了松散模式。(我知道您使用的是松散模式,因为如果您使用的是严格模式,
将是
未定义的
,因此
此。另一个函数=…
将失败。)



旁注:我建议您停止使用
public
作为变量名,因为它是一个变量名,至少从ES3开始就已经存在了。

首先,您创建“类”的方式是不正确的,最终会创建全局变量:在匿名函数的调用中,因为您调用它的方式,
this
将引用全局对象*,因此
this.anotherFunction=…
将创建一个名为
anotherFunction
的全局变量,因为全局对象上的属性是全局变量

如果您想继续使用当前模式进行最小的更改,则不要在函数中使用
this.xyz=…
,而是使用
var

var search = function()
{
   var anotherFunction = function(param1, param2)
   {
      // do whatever
   };

   var public = { anotherFunction: anotherFunction }  

   return public;
}();
还请注意,您没有声明
search
,这是您的牺牲品;我添加了一个
var
来声明它

如果您没有调用最外层的函数,只是将该函数分配给
search
变量,然后稍后调用它,那么您的第二个示例(具有上述更改)将起作用:

var search = function(url)
{

   var anotherFunction = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var anotherFunctionB = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var public = { anotherFunction: anotherFunction,
                  anotherFunctionB: anotherFunctionB }  

   return public;
}; // <== Note, no () here

*为什么调用匿名函数时,
引用全局对象?因为你调用它时没有做任何事情来设置
这个
,而是使用了松散模式。(我知道您使用的是松散模式,因为如果您使用的是严格模式,
将是
未定义的
,因此
此。另一个函数=…
将失败。)



旁注:我建议您停止使用
public
作为变量名,因为它是一个变量名,至少从ES3开始就已经存在了。

首先,您创建“类”的方式是不正确的,最终会创建全局变量:在匿名函数的调用中,因为您调用它的方式,
this
将引用全局对象*,因此
this.anotherFunction=…
将创建一个名为
anotherFunction
的全局变量,因为全局对象上的属性是全局变量

如果您想继续使用当前模式进行最小的更改,则不要在函数中使用
this.xyz=…
,而是使用
var

var search = function()
{
   var anotherFunction = function(param1, param2)
   {
      // do whatever
   };

   var public = { anotherFunction: anotherFunction }  

   return public;
}();
还请注意,您没有声明
search
,这是您的牺牲品;我添加了一个
var
来声明它

如果您没有调用最外层的函数,只是将该函数分配给
search
变量,然后稍后调用它,那么您的第二个示例(具有上述更改)将起作用:

var search = function(url)
{

   var anotherFunction = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var anotherFunctionB = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var public = { anotherFunction: anotherFunction,
                  anotherFunctionB: anotherFunctionB }  

   return public;
}; // <== Note, no () here

*为什么调用匿名函数时,
引用全局对象?因为你调用它时没有做任何事情来设置
这个
,而是使用了松散模式。(我知道您使用的是松散模式,因为如果您使用的是严格模式,
将是
未定义的
,因此
此。另一个函数=…
将失败。)



旁注:我建议您停止使用
public
作为变量名,因为它是一个变量名,至少从ES3开始就已经存在了。

首先,您创建“类”的方式是不正确的,最终会创建全局变量:在匿名函数的调用中,因为您调用它的方式,
this
将引用全局对象*,因此
this.anotherFunction=…
将创建一个名为
anotherFunction
的全局变量,因为全局对象上的属性是全局变量

如果您想继续使用当前模式进行最小的更改,则不要在函数中使用
this.xyz=…
,而是使用
var

var search = function()
{
   var anotherFunction = function(param1, param2)
   {
      // do whatever
   };

   var public = { anotherFunction: anotherFunction }  

   return public;
}();
还请注意,您没有声明
search
,这是您的牺牲品;我添加了一个
var
来声明它

如果您没有调用最外层的函数,只是将该函数分配给
search
变量,然后稍后调用它,那么您的第二个示例(具有上述更改)将起作用:

var search = function(url)
{

   var anotherFunction = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var anotherFunctionB = function(param1, param2)
   {
      // use here the 'url' parameter
   };

   var public = { anotherFunction: anotherFunction,
                  anotherFunctionB: anotherFunctionB }  

   return public;
}; // <== Note, no () here

*为什么调用匿名函数时,
引用全局对象?因为您调用它时没有做任何设置
this