javascript - Call a function of instance itself when it is initialized -
i newbie react.js , found 1 tricky thing that stucks me.
i trying use 1 function of class when create instance of class, seems not able this.
to more specific:
i have class called personcalendar
, in class, have function called calculatedsalary
also, in class, have attribute called salary
.
now trying create instance of class doing following:
var personcal = new personcalender({salary: personcal.calculatedsalary(138)})
i tried this:
var personcal = new personcalender({salary: this.calculatedsalary(138)})
neither of them working.
when execute code, code stop on line saying function undefined.
can give me suggestion on how deal tricky situation?
many thanks
this has nothing react. cannot access before exists. hence cannot access function of instance before instance exists.
i don't know personcalender
doing or how processing arguments, seems want calculate salary inside constructor:
function personcalender(somenumber) { this.salary = this.calculatedsalary(somenumber); }
or make calculatedsalary
static if need access instance:
personcalender.calculatedsalary = function(...) { ...}; var personcal = new personcalender({salary: personcalender.calculatedsalary(138)})
i recommend read mdn article oop in js make more familiar topic.
Comments
Post a Comment