0

i have showProfile function which is accept callback function .

The callback function has also an argment which is an object.

showProfile(call_back_fn) 

invoke it :

showProfile(function(obj){console.log(obj)})

I want to set this function as an attribute to Person Object :

    function Person(name,age,showProfile){
       this.name=name ;
        this.age=age;
       /*
        this.showProfile=showProfile ???
                 OR
       this.prototype.showProfile=showProfile ???
               OR What ??    
*/

              return this;        
    }

then ,to create a Person object :

 var p=new Person('Abdennour',23,fnshowProfile);

How can i make this function(showProfile) as an object attribute .and if it's possible , how to pass its argument(wich is a call back function) ? Then , How to Call fnshowProfile which is in the snippet above.

UPDATE

OtherWise, if i create a Person object as follwoing :

 var p=new Person('Abdennour',23,showProfile());

How can i access to showProfile to add a callback function as argument: an d then , how can i execute showProfile from p object.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254

1 Answers1

0

Yes, I found it :
Demo :http://jsfiddle.net/abdennour/5vNQ5/

callbackshowProfile=function(obj){
    alert(obj['field1']);

}

showProfile=function(cfn){
     cfn();
}


function Person(name,age,shfn,callback){
 this.name=name;
    this.age=age;
    this.shfn=shfn;
    this.callback=callback;
    return this;
}

Create a Person object :

var pp=new Person('Abdennour',13,showProfile,callbackshowProfile)

To call showProfile which has callback function as parameter, which is has an object as parameter.

pp.shfn(pp.callback.bind(null,{field1:'My field 1'}))
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254