0

我将如何调用一个在对象内部发出警报的函数?这是我到目前为止所得到的:

function ObjectAlert(a,b,c){
  var obj = {};
  obj.name = a;
  obj.location = b;
  obj.shout= function (){alert(c);};
  return obj;

}
ObjectAlert("jake","here","alert");
4

6 回答 6

3
ObjectAlert("jake","here","alert").shout();

或者

var OA = ObjectAlert("jake","here","alert");
OA.shout();

?

于 2013-10-10T06:23:06.023 回答
1

试试下面的代码。希望它能满足你的要求。。

function ObjectAlert(a,b,c){
  var obj = {};
  obj.name = a;
  obj.location = b;
  obj.shout= function (){alert(c);};
  return obj.shout();

}
ObjectAlert("jake","here","alert");

或者,您可以尝试以下方法..

function ObjectAlert(a,b,c){
  var obj = {};
  obj.name = a;
  obj.location = b;
  obj.shout= function (){alert(c);};
  return obj;

}
ObjectAlert("jake","here","alert").shout();

而且,也可以试试这个..

function ObjectAlert(a,b,c){
  var obj = {};
  obj.name = a;
  obj.location = b;
  obj.shout= function (){alert(c);};
  return obj;

}
var shout = ObjectAlert("jake","here","alert");
shout.shout();
于 2013-10-10T06:36:32.717 回答
0

尝试这个,

var object = new ObjectAlert("jake","here","alert");
object.shout();
于 2013-10-10T06:23:57.787 回答
0

简直喜欢

var test = ObjectAlert("this","is","alert");
test.shout();

甚至更好/直接

ObjectAlert("this","is","alert").shout();
于 2013-10-10T06:25:22.937 回答
0
function ObjectAlert(a,b,c){
  var obj = {};
  obj.name = a;
  obj.location = b;
  obj.shout= function (){alert(c);};
  return obj;   
}

var myAlert = ObjectAlert("jake","here","alert");
myAlert.shout();
于 2013-10-10T06:23:19.793 回答
0

希望这可以帮助:

var obj = ObjectAlert('jake', 'here', 'alert');
obj.shout();
于 2013-10-10T06:31:11.113 回答