Answer the question
In order to leave comments, you need to log in
JavaScript: Passing not just a function, but an entire object with its properties?
function TestClass(prop) {
if (prop) {
parms = prop;
}
}
TestClass.prototype = {
constructor: TestClass,
parms: 'test',
var1: 2,
fnc: function () {
alert(this.var1);
}
}
function fnc2(msg) {
test = new TestClass('11');
var myFnc = test.fnc;
myFnc();
}
Answer the question
In order to leave comments, you need to log in
There are no classes in JS, and this is not a reference to an object, but an execution context. You can use closures if you need some properties inside objects, like this:
function foo(param){
var a = param;
this.getA = function(){
return a;
};
this.setA = function(p){
a = p;
};
}
var f = new foo(123);
g = f.getA();
console.log(g());
You need to fix the execution context of some function. This is done using Function.prototype.bind from ES5. There are polyfills in underscore.js and jQuery - _.bind and $.proxy respectively.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question