Sunday, 8 September 2013

JS cannot access object value from different function

JS cannot access object value from different function

Can anyone axplain why the first thing works and the second dont?
function MyObj(val) {
this.value = val;
this.myFunc = function(selector) {
$(selector).html("test: " + this.value);
}
}
foo = new MyObj("tester");
foo.myFunc("#one"); //This works
func = foo.myFunc;
func("#two"); //This doesnt
How? and how can i make it work?
JSFIDDLE

No comments:

Post a Comment