JavaScript Revealing Module Pattern global variable is undefined - javascript

I'm trying to use the Revealing Module Pattern to scope the JavaScript on my page so that I don't pollute the global namespace.
<script type="text/javascript">
var myModule = (function(){
function windowLoad() {
// window onLoad things
return;
}
function otherFunc(){
// otherFunc things
}
window.onload = windowLoad;
return { otherFunc: otherFunc };
})();
myModule.otherFunc(); // myModule is undefined here
</script>
For some reason, as shown in the comment above, myModule is undefined when I go to use it. Why?

myModule is not undefined. It is the object you returned from within your immediately invoked function; What is undefined is the result of calling myModule.otherFunc because that function doesn't return anything.
See the following snippet for an explanation.
var myModule = (function() {
function windowLoad() {
// window onLoad things
return;
}
function otherFunc() {
return 1;
}
window.onload = windowLoad;
return {
otherFunc: otherFunc
};
})();
console.log(myModule); // object {otherFunc: function otherFunc(){...}}
console.log(myModule.otherFunc()); // 1

As others have mentioned, your code works as written. As you've written it, otherFunc returns undefined since it doesn't have a return statement to explicitly return a value.
When you're debugging something like this, it's best to examine both the object and function you're calling separately:
console.log(myModule); // should print [Object object]
console.log(myModule.otherFunc); // should print a function definition
You could also try adding a return to otherFunc:
function otherFunc() {
return "This is otherFunc's return."
}

Related

I don’t undestand why my JS function results in an error

I am learning Javascript, and I don't understand why this JS function results in an error.
function fun(){
console.log('foo');
return {
fun: function(){
fun();
}
};
}
fun().fun(); // 'foo','foo'
fun().fun().fun(); // Error: Cannot read property 'fun' of undefined
fun().
Any time a function is called that does not return something the default return is always provided by the JavaScript runtime. The default return is undefined. Therefore, when you attempt to call 'fun()' the third time, an attempt is made to find an Object property called 'fun', but the previous call simply return undefined - hence: Cannot read property 'fun' of undefined
So try this instead:
function fun(){
console.log('foo');
return {
fun: function(){
return fun();
}
};
}
fun().fun();
fun().fun().fun();
This returns the output of the function call - which is the intended Object.

Understanding function self-referencing mechanics

I had (approximately) this situation: I was trying to declare func1() function and add couple of static methods to it in one go by running it through _init() function providing a hash with properties to attach to it, not being aware of that it didn't remain declared in current scope after _init() function did it's job. It just got temporarily defined, and gc-ed after _init() run (as far as I'm aware). Here's the code sample:
//
// #_init
// merge src{} into target{}, overwriting
//
var _init = (function (target, src) {
var _ = this;
if (_.isobj(target))
_.keys(src).forEach(_.pass, {
src : src,
target : target
});
return target;
}).bind({
isobj : (function (node) {
return node === this(node);
}).bind(Object),
keys : (function (node) {
return this.keys(this(node));
}).bind(Object),
pass : function (field) {
this.target[field] = this.src[field];
}
});
, and I was hoping to 'batch-init' it here adding static methods at the same time:
_init(function func1 (e) {
var _api = func1.pop(arguments);
var node = this;
// and stuff...
}, {
pop: Function.prototype.call.bind(Array.prototype.pop),
// ...etc
});
When trying to reference it later I got error:
x = func1();
// ReferenceError: func1 is not defined
// x = func1()
Assigning the output of _init() to var func2 does the job, I can reference and use the function. Thing that confuses me is when console.log()-ing the func2 logs 'func1()', but trying to refernce func1 directly throws ReferenceError:
//
// #func2
//
var func2 = _init(function func1 () {
return func1.pop(arguments);
}, {
pop: Function.prototype.call.bind(Array.prototype.pop)
});
console.log(typeof func2, func2, func2(1,2,3));
// function func1() 3
console.log(func1(1,2,3));
// ReferenceError: func1 is not defined
// console.log(func1(1,2,3));
//
Could someone explain to me why the func1 reference didn't get created, but (strangely) was availble to func2 (it obviously was able to use it...)?
Could someone explain to me why the func1 reference didn't get created, but (strangely) was availble to func2 (it obviously was able to use it...)?
That's just how named function expressions work. Their name (func1) is available inside the function body as an identifier, but not outside. What happens to the result of the expression (passing the created function to _init, then assigning it to func2) is a different and completely unrelated thing.
I was trying to declare func1() function and add couple of static methods to it
You shouldn't, unless those static methods are really supposed to be public and not just simple helper methods. Use the revealing XY pattern (IEFE) and just get your utility functions in the closure scope:
var _init = (function() {
function isobj(node) {
return node === Object(node);
}
function pass(field) {
this.target[field] = this.src[field];
}
var keys = Object.keys;
return function (target, src) {
if (isobj(target))
keys(src).forEach(_.pass, {
src : src,
target : target
});
return target;
};
});
var func2 = (function() {
var pop = Function.prototype.call.bind(Array.prototype.pop);
return function func1 () {
return pop(arguments);
};
});

Javascript calling prototype functions in constructor

I keep getting an error saying that my functions are not defined when I was trying to call the prototype functions in the constructor and I dont know whats wrong with it.
Here's the code I have:
function Renderer()
{
initialiseWebGL();
initialiseShader();
initialiseBuffer();
}
Renderer.prototype.initialiseWebGL()
{
//Do stuff.
};
Renderer.prototype.initialiseShader()
{
//Do Shader's stuff
};
Renderer.prototype.initialiseBuffer()
{
//Do Buffers
};
What is wrong with it?
Your syntax is wrong. Use this:
function Renderer() {
this.initialiseWebGL();
this.initialiseShader();
this.initialiseBuffer();
}
Renderer.prototype.initialiseWebGL = function () {
//Do stuff.
};
Renderer.prototype.initialiseShader = function () {
//Do Shader's stuff
};
Renderer.prototype.initialiseBuffer = function () {
//Do Buffers
};
After that you can create new object and use it by:
var rendererInstance = new Renderer();
There are a few things wrong with your Code
1.initialiseWebGl() would look for a function declared in the Global scope -> there is no function
You should use this.initialiseWebGl() to access the Objects Method
Note: this refers to the Instance of Renderer in this case
2.You are not assigning a function with Renderer.prototype.initialiseWebGL() instead you try to invoke the Renderers prototype method initialiseWebGl which gives you an error, as its not defined
3.Because the { are moved down a line they get interpreted as a Block -> this code gets executed.
If you'd had them after your () you would get a Syntax Error ->
Renderer.prototype.initialiseWebGL() {... would result in Uncaught SyntaxError: Unexpected token {
Heres an Commented Example
function Renderer() {
initialiseWebGL(); // I call the global declared function
this.initialiseShader(); //I call the Prototypes function
this.initialiseBuffer(); //Me too
}
Renderer.prototype.initialiseWebGL = function (){ //Here a function gets assigned to propertie of `Renderer`s `prototype` Object
//Do stuff.
};
Renderer.prototype.initialiseShader = function (){
console.log("Do Shader Stuff");
};
Renderer.prototype.initialiseBuffer = function (){
console.log("Do initialise stuff");
};
Renderer.prototype.initialiseBuffer() // I invoke the method above
{
console.log("I'm a Block statement");
};
function initialiseWebGL () { //I'm the global declared function
console.log("Global");
}
var ren1 = new Renderer();
/*"Do initialise stuff"
"I'm a Block statement"
"Global"
"Do Shader Stuff"
"Do initialise stuff"*/
As you can see in the consoles Output
Heres a JSBin
Since your instances inherit the (method) properties from the prototype objects, you need to access them as properties and not as plain variables:
function Renderer() {
this.initialiseWebGL();
this.initialiseShader();
this.initialiseBuffer();
}

JavaScript: How do use instance variables

why doesn't this work as expected. (see expected comment)
var Module = function () {
var public_instance_var;
function doStuff () {
Module.doOtherStuff();
console.log(public_instance_var); // expected: true, but logs undefined
};
function doOtherStuff() {
public_instance_var = true;
};
return {
public_instance_var: instance_var,
doStuff: doStuff,
doOtherStuff: doOtherStuff
}
}();
Module.doStuff();
Update: Fixed accordingly to a few of jAndy suggestions
Multiple errors here:
You don't return DoStuff as module interface
instance_var is not declared, probably meant public_instance_var
doOtherStuff is never assigned to Module, just call it like doOtherStuff();
Fixed code:
var Module = function () {
var public_instance_var;
function doStuff() {
doOtherStuff();
console.log(public_instance_var); // expected: true, but logs undefined
};
function doOtherStuff() {
public_instance_var = true;
};
return {
doStuff: doStuff,
public_instance_var: public_instance_var
}
}();
Module.doStuff();
change your code like so
var Module = function () {
var public_instance_var;
function doStuff () {
doOtherStuff();
console.log("var is ", public_instance_var); // expected: true, but logs undefined
};
function doOtherStuff() {
public_instance_var = true;
};
return {
public_instance_var: public_instance_var,
doStuff : doStuff
}
}();
Module.doStuff();
you have to return doStuff() function (otherwise outside it will be undefined) and public_instance_var instead of instance_var
you need to execute doOtherStuff() without prefixing Module.
What this code does is, simply put: create and run a function and assign its return value to a variable: Module. The return value is an object with 1 property: public_instance_var, that points to the variable instance_var, or (after correcting the typo: public_instance_var). This variable was declared, but not instantiated. Therefore the return value looks like this:
Module.public_instance_var = undefined
The very last line Module.doStuff(); won't work one bit: Module is an object that has no methods. The functions you declared are garbage collected when the anonymous function returns. If you want access to those functions, you'll need to include them in the return statement. Read up on closures, Object constructors and design patterns in general, but I'd say the code you're after will look something like this:
var Module = (function()
var public_instance_var;
function doStuff () {
this.doOtherStuff();
console.log(public_instance_var); // expected: true, but logs undefined
};
function doOtherStuff() {
public_instance_var = true;
};
return {
public_instance_var: public_instance_var,
doStuff: doStuff,
doOtherStuff: doOtherStuff
};
})();
Of course, this way your variable public_instance_var is a public property, so my guess would be what you're really trying to do is simulate a private properties and methods. In which case you might end up with code similar to this:
var Module = (function()
{
var public_instance_var;
return {
//public_instance_var: public_instance_var, remove this line
//the closure will preserve access to the variable
doStuff: function ()
{
this.doOtherStuff();//this, you're referencing the object's property
console.log('here I am');
},
doOtherStuff: function ()
{
public_instance_var = true;
//this won't work anymore:
//this.public_instance_var = true;
};
}
})();
Module.doStuff() now logs here I am, but the doOtherStuff is now a public method, too. Here's how you might choose to solve the issue:
var Module = (function()
{
var public_instance_var;
function doOtherStuff ()
{
public_instance_var = true;
};
return {
//public_instance_var: public_instance_var, remove this line
//the closure will preserve access to the variable
doStuff: function ()
{
doOtherStuff();//don't use this here, but the reference to the function exists thanks to closure
console.log('here I am');
console.log(public_instance_var);//logs true
}
};
})();
These are just a few of the very powerful things you can do with closures and functions returning objects. Just read a couple of articles, like this one, there are better ones out there. Google the term power constructors

javascript - shortcut for calling a function at the same time it is defined

to call a function at the same time it's defined, i had been using:
var newfunc = function() {
alert('hi');
};
newfunc();
is the following the correct way of combining these 2:
var newfunc = function() {
alert('hi');
}();
There could be a number of reasons you wish to do this. I'm not sure what yours are, but let me introduce a couple of favourite patterns:
Pattern #1: A singleton. The function is executed and then becomes a singleton object for use by other components of your code.
var singletonObject = new function() {
// example private variables and functions
var variable1 = {};
var variable2 = {};
var privateFunction = function() {
};
// example public functions
this.getData = function() {
return privateFunction(variable1, variable2);
};
// example initialisation code that will only run once
variable1.isInitialised = true;
};
Pattern #2: Self-executing anonymous function ... handy for sooo many reasons!
// Declare an anonymous function body.
// Wrap it in parenthesis to make it an "expression.
// Execute it by adding "();"
(function(){})();
And here's an example that also creates a namespace for your objects.
I'm using "NS" as an example namespace:
// declare the anonymous function, this time passing in some parameters
(function($, NS) {
// do whatever you like here
// execute the function, passing in the required parameters.
// note that the "NS" namespace is created if it doesn't already exist
})(jQuery, (window.NS = window.NS || {}));
You can also set the context of a self-executing function by using .call or .apply instead of the usual parenthesis, like this:
(function($){
// 'this' now refers to the window.NS object
}).call(window.NS = window.NS || {}, jQuery);
or
(function($){
// 'this' now refers to the window.NS object
}).apply(window.NS = window.NS || {}, [jQuery]);
var newfunc = function f() {
alert("hi!");
return f;
}();
Having a named function expressions allows the function to recursively call itself or, in this case, return itself. This function will always return itself, however, which might be an annoyance.
No. Your second example will immediately call the anonymous function and assign its return value to newfunc.
adamse describes an approach which appears to work. I'd still avoid the approach as the two step process is easier to read and thus will be easier to maintain.
If I understand your question correctly, give this a try:
(f = function (msg) {
msg = msg ? msg : 'default value';
alert(msg); }
)();
f('I\'m not the default value!');
You'll get two alerts, the first one will say "default value" and the second will say "I'm not the default value. You can see it in action at jsBin. Click 'preview' to make it run.
you could do like this:
o = {};
o.newfunc = ( function() {
function f() {
alert('hi');
}
f();
return {
f : f
};
}
)();
then calling the function like:
o.newfunc.f();
will also render an alert message

Categories

Resources