我如何调用对象中的函数

时间:2013-10-10 06:20:39

标签: javascript

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

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

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

6 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分: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();

答案 2 :(得分: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();

答案 3 :(得分:0)

试试这个,

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

答案 4 :(得分:0)

简单地喜欢

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

甚至更好/直接

ObjectAlert("this","is","alert").shout();

答案 5 :(得分:0)

希望这会有所帮助:

var obj = ObjectAlert('jake', 'here', 'alert');
obj.shout();