In the below code
(function x() {
var j = function() { alert("234234"); }
return {
s: j
}
})()
var x1 = new x();
x1.s();
How to invoke the method j()? Or before that i should be asking, is there a way to create an instance of the function x() because that is what is happening. Please see the fiddle /
In the below code
(function x() {
var j = function() { alert("234234"); }
return {
s: j
}
})()
var x1 = new x();
x1.s();
How to invoke the method j()? Or before that i should be asking, is there a way to create an instance of the function x() because that is what is happening. Please see the fiddle http://jsfiddle/e5k6bdvn/
Share Improve this question asked Jan 23, 2015 at 18:12 maze starmaze star 111 silver badge3 bronze badges 5- 1 Why do you have it like that to begin with? That pattern does not make sense. – epascarello Commented Jan 23, 2015 at 18:14
- 1 You code is very odd. You seem to have built something following the module pattern (except you used a named function), and then you invoke the function that should only be self-invoked again but as if it was a constructor function (which it definitely isn't designed to be) – Quentin Commented Jan 23, 2015 at 18:18
- True! you caught that. the whole idea is not to make it a constructor function. but rather am trying to find a way to invoke method j inside it. By the way, self invoking functions cannot be a constructor function?? – maze star Commented Jan 23, 2015 at 18:20
- It makes no sense to write a self-invoking constructor function. Constructor functions are good for creating multiple instances of an object type. Self-invoking functions are good for creating singletons. – Quentin Commented Jan 23, 2015 at 18:21
- Allrite, I get the point. My question still remains unanswered. Can self invoking functions be a constructor function? since i am getting error while creating an instance. – maze star Commented Jan 23, 2015 at 18:24
2 Answers
Reset to default 8- Don't give the self invoking function a name (there is little benefit to it beyond making it easier to find in a stacktrace, which you shouldn't need at that point, and it memory leaks in old IE)
- Don't call the self invoking function twice
- Don't treat the self invoking function as if it was a constructor function (by calling it with
new
) - Do capture the return value of the self-invoking function
Such:
var x = (function () {
var j = function() { alert("234234"); };
return {
s: j
};
})();
x.s();
Or, if you want to create multiple objects in the same way:
- Don't use a self-invoking function
- Do call
x
as many times as you like
Such:
function x () {
var j = function() { alert("234234"); };
return {
s: j
};
};
var x1 = x();
var x2 = x();
x1.s();
Or, if you want to create a constructor function:
- Don't return anything from it
- Do use the prototype chain
Such:
function x () {
};
x.prototype.s = function () {
alert("234234");
}
var x1 = new x();
var x2 = new x();
x1.s();
As stated by @epascarello, it's hard to say what is the context of your question.
If the meaning of x
is to return objects more than once, you should not immediately invoke it. Instead you need only to declare it:
function x () { return { j: function () { alert("234234") } } }
then call it whenever you want, and invoke j
.
var x1 = x();
x1.j();
If instead you're planning to use x
only once, it's nice to invoke it immediately, but you need to consume the call to j
immediately too.
(function () {
return {
j: function () {
alert("234234")
}
}
})().j();