Explain this please: var MYLIBRARY = MYLIBRARY || (function(){ [duplicate] - javascript

This question already has answers here:
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 8 years ago.
I've stumbled upon construct that I'm not sure what it does
var MYLIBRARY = MYLIBRARY || (function(){
https://stackoverflow.com/a/2190927/680815
I don't have much rep. yet so I can't post a comment to ask about so well, sorry for the mess. :)
Does it mean if MYLIBRARY is defined use it and if not assign encapsulated code?
Thanks,

yes, it does pretty much what you think.
if MYLIBRARY is defined it is used, if not it is assigned the encapsulated code?

that checks if MYLIBRARY not undefined, null or false then keep it as it is, else it will be the function assigned.
in another words:
if (!MYLIBRARY) {
MYLIBRARY = function(){};
}
but in your snippet I think MYLIBRARY is always undefined because you're setting the variable when you check or it's duplicating.

Related

Why does it throws reference error for b? [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Recently I attended a interview for Frontend development, The interviewer came up with a problem which made me doubt my confidence on javascript. The question is
function some() {
console.log(a) // undefined
console.log(b) // Reference Error: cannot access b before initialization.
var a = 10;
let b = 15
}
I understood hoisting is happening here, but i am not sure why b throwed reference error. Could anyone please explain ?
Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Why hoisting works differently in variables compared to functions? [duplicate]

This question already has answers here:
Surprised that global variable has undefined value in JavaScript
(6 answers)
Closed 2 years ago.
I have been reading some docs lately and came across some inconsistent patterns related to hoisting in JavaScript.
It goes as follows:
In functions,
function abc(){
console.log("worked")
}
abc();
OUTPUT : worked
Also,
abc();
function abc(){
console.log("worked")
}
OUTPUT : worked
This happens due to hoisting as in the execution context the memory is saved initially for abc.
But the same doesnt happen in case of varibles and I was wondering why
For example,
var a = "Hello"
console.log(a)
OUTPUT : Hello
**So why not the same in given below code?
console.log(a)
var a = "Hello"
The above code states "undefined"
when the execution starts for the above code:
a is set to undefined
then undefined is reassigned to the value provided that is "Hello"
But that is not the case, instead it outputs undefined
Why is that?
Thank you for reading.
Any help would be really appreciated.
The code
console.log(a)
var a = "Hello"
is interpreted as if it were written
var a;
console.log(a);
a = "Hello"
Why? That's the way the language is specified to work. Hoisting the whole initialization expression would create problems in many cases.

Why can I delete a variable declared a = 1 and not one declared var b = 1 in javascript [duplicate]

This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 7 years ago.
Let's suppose we are in the global scope:
When I declare a variable in JS:
a = 1
which I know is not the proper way (but it is not the question).
I can use:
delete a (> returns true)
But when I declare:
var b = 1
I can't use:
delete b (> returns false)
Can anyone explain that behaviour?
delete is only effective on an object's properties. It has no effect on variable or function names.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
a becomes/is ultimately interpreted as window.a, while b clearly refers to a local variable.

Javascript: this.varName = "" vs var varName = "" [duplicate]

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Can anyone tell me what this javascript might be doing [duplicate]

This question already has answers here:
What does "options = options || {}" mean in Javascript? [duplicate]
(5 answers)
Closed 9 years ago.
So I am looking over a project which includes the following line of javascript:
window.negotiationApp = window.negotiationApp || {};
Can anyone explain might be going on with this line of code?
Update
So now that I understand what this line of code is doing, my question maybe unique in asking the following:
There is no negotiationApp object in the javascript code. window.negotiationApp will always be set to an empty object, it seems the developer is really just using this as a namespace or container for other objects. Is this a common javascript practice?
it makes sure that window.negotiationApp is set. If window does not have this property then it initializes it to be {} (an empty object), otherwise, it does nothing.
It's an idiom that basically means, if window.negotiationApp doesn't exist, set it to {}. You might do that so future info doesn't return undefined or something.
Ensures window.negotiationApp object is not undefined.
window.negotiationApp = window.negotiationApp || {};
Means if window.negotiationApp is defined then use it or assign window.negotiationApp an empty object.
if(window.negotiationApp) {
window.negotiationApp = window.negotiationApp;
}
else {
window.negotiationApp = {};
}
since this variable is set on the global scope, it makes sure not to override an existing one if there is any.
so it basically says, if there is already a negotiationApp variable defined - use it, if not create a new one.

Categories

Resources