在javascript中将对象作为参数传递给函数

时间:2022-12-05 17:11:32

I am trying to pass an object to a function but when I try to output the object it says "undefined". Here is an example:

我试图将一个对象传递给一个函数,但是当我尝试输出该对象时,它表示“未定义”。这是一个例子:

// object defined
this.object = new Ext.data.JsonStore({
  //some store properties
});

// printing object
function printObject (obj) {
   alert(obj); // my output is "undefined"
}

// trying to pass the object
printObject(this.object);

Can anyone tell me how can I pass an object as an argument? thanks in advance

任何人都可以告诉我如何将对象作为参数传递?提前致谢

3 个解决方案

#1


1  

what is the context here, what does this refer to, another object? try calling it something other than 'object', perhaps 'data'. not 100% sure, but I think 'object' may be reserved.

这里的上下文是什么,这是指什么,另一个对象?尝试称之为“对象”以外的东西,也许是“数据”。不是100%肯定,但我认为'对象'可能会被保留。

#2


0  

possibly it is the function pringObject (rather than printObject) that is undefined.

可能是未定义的函数pringObject(而不是printObject)。

#3


0  

Try this:

var obj = new Ext.data.JsonStore({
  //some store properties
});

So that obj is implicitly casted to an object of type returned by JsonStore

因此obj被隐式地转换为JsonStore返回的类型的对象

#1


1  

what is the context here, what does this refer to, another object? try calling it something other than 'object', perhaps 'data'. not 100% sure, but I think 'object' may be reserved.

这里的上下文是什么,这是指什么,另一个对象?尝试称之为“对象”以外的东西,也许是“数据”。不是100%肯定,但我认为'对象'可能会被保留。

#2


0  

possibly it is the function pringObject (rather than printObject) that is undefined.

可能是未定义的函数pringObject(而不是printObject)。

#3


0  

Try this:

var obj = new Ext.data.JsonStore({
  //some store properties
});

So that obj is implicitly casted to an object of type returned by JsonStore

因此obj被隐式地转换为JsonStore返回的类型的对象