Javascript 将函数(具有回调函数(具有参数))作为对象属性传递
我有showProfile函数,它是接受回调函数 回调函数还有一个参数,它是一个对象Javascript 将函数(具有回调函数(具有参数))作为对象属性传递,javascript,callback,jsobject,Javascript,Callback,Jsobject,我有showProfile函数,它是接受回调函数 回调函数还有一个参数,它是一个对象 showProfile(call_back_fn) 调用它: showProfile(function(obj){console.log(obj)}) 我想将此函数设置为Person对象的属性: function Person(name,age,showProfile){ this.name=name ; this.age=age; /*
showProfile(call_back_fn)
调用它:
showProfile(function(obj){console.log(obj)})
我想将此函数设置为Person对象的属性:
function Person(name,age,showProfile){
this.name=name ;
this.age=age;
/*
this.showProfile=showProfile ???
OR
this.prototype.showProfile=showProfile ???
OR What ??
*/
return this;
}
然后,要创建Person对象,请执行以下操作:
var p=new Person('Abdennour',23,fnshowProfile);
var pp=new Person('Abdennour',13,showProfile,callbackshowProfile)
如何将此函数(showProfile)设置为对象属性。如果可能,如何传递其参数(它是回调函数)?
然后,如何调用上面代码段中的fnshowProfile
更新
否则,如果我创建Person对象,如下所示:
var p=new Person('Abdennour',23,showProfile());
如何访问showProfile以添加回调函数作为参数:
然后,如何从p
对象执行showProfile。是的,我找到了:演示: 创建Person对象:
var p=new Person('Abdennour',23,fnshowProfile);
var pp=new Person('Abdennour',13,showProfile,callbackshowProfile)
调用以回调函数作为参数的showProfile,该函数以对象作为参数
pp.shfn(pp.callback.bind(null,{field1:'My field 1'}))
这有点不清楚,你能为这个做点什么吗?我编辑了问题的标题:传递函数(has callback function(has argument))作为对象属性。另请参见更新后编写的单词