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

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.

Related

Best approach for determining if an object in reference is null [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
JavaScript, elegant way to check nested object properties for null/undefined [duplicate]
(4 answers)
Closed 5 years ago.
In JavaScript (or TypeScript), if I'm going to reference something like:
return myObj1.myObj2.myObj3.myProperty;
to be safe, I guard it with something like:
if (myObj1 && myObj2 && myObj3)
return myObj1.myObj2.myObj3.myProperty;
else
return null;
Is there a more concise way of just getting null from the above reference without surrounding it by an if?
Please note, since I'll be using this in a TypeScript app, some solutions may not work with dynamic object definition.
Thanks.
I'd use a library like lodash for this:
//gets undefined if any part of the path doesn't resolve.
const answer = _.get(myObj1, "myObj2.myObj3.myProperty");
//gets null if any part of the path doesn't resolve.
const answer2 = _.get(myObj1, "myObj2.myObj3.myProperty", null);
See https://lodash.com/docs/4.17.4#get for details.
You can define some temporary objects to substitute when a lookup fails.
return (((myObj1||{}).myObj2||{}).myObj3||{}).myProperty || null;
You can make a reusable object if desired.
const o = Object.create(null);
Then use it as needed.
return (((myObj1||o).myObj2||o).myObj3||o).myProperty || null;

Define & assign variable with function Node [duplicate]

This question already has answers here:
Does node.js have equivalent to window object in browser
(2 answers)
Closed 6 years ago.
I know with browser-run JavaScript, I could use window[varName]=value; to set global variables. I seem to remember there being a function to accomplish this in Node JS, but I'm not sure what it is.
If it helps, I'm aiming to set all the properties of an object as separate own variables.
In Node.js the global variables are stored in the global object, I think...
Then it'd be so:
global[varName] = value;
// or...
global.varName = value;
I think you're confusing something. Maybe you want to initialize a object constructor expression.
{ property: "string, or anything else" }
// this expression returns a object that can be assigned
// everywhere, but when assigned, turns a reference
If you want to get/set properties in this object you must do the same thing you were doing before, index the object with the keys [...] or ., then you can optionally assign (set) the object's property with =, else it'll be returned.

Check if object inside an object is empty with javascript [duplicate]

This question already has answers here:
How do I test for an empty JavaScript object?
(48 answers)
Closed 8 years ago.
Hello I have an object:
var equippedItems = {
weapon: {},
armor: {},
accessory: {}
};
I need a way to check if equippedItems.weapon equals to '' at some point I am doing something like equippedItems.weapon = ''; I dont know if it's exactly the same as above object. I already tried using object.hasOwnProperty but it seems I cannot use equippedItems.weapon in this case, maybe I am doing something wrong? Also note I did read how to check if object is empty already, but it didn't work for my object inside an object.
#Edit:
Like I said, I already read those and they did not give me a straight answer for my question, they did answer how to check if object is empty, but the object was like object = {}; while mine is like
object = {
object:{},
object2:{},
object3:{}};
Thats why it confuses me.
Just make use of Object.keys
function isEmpty(obj, propName){
return Object.keys(obj[propName]).length == 0;
}
Another way is to make use of JSON.stringify method which will return {} if empty
function isEmpty(obj, propName){
return JSON.stringify(obj[propName]) == "{}";
}
In both the cases, you would call the function like
if(isEmpty(equipmentItems.weapons)){
equipmentItems.weapons = "";
}
To check that an object has a property you can use:
"weapon" in equippedItems
or
equippedItems.hasOwnProperty("weapon")

why can't I assign a property value to a following property in an object declaration? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I'm learning Javascript and I've come across something I don't understand. This is the part of my object code:
var monster =
{
...
//is the animation starting?
hiding: true,
delayDuration: Math.floor(Math.random() * 60),
currentDelay: this.delayDuration,
...
};
If I console.log (delayDuration) I get a value, but if I console.log (currentDelay) it says 'undefined'.
I don't understand why currentDelay doesn't take the value of delayDuration.
Can someone please explain this?
edit: #Bergi why did you mark this as duplicate? I couldn't find my question answered somewhere else
edit2: yup, it's a duplicate. At least now I know the words for what I was asking.
At the point of object creation neither monster nor any of it's properties are defined. You can't use variable from the same object you're in the process of constructing.
Also, Javascript uses function scoping, which means that the value of this will either be the window object or will be scoped to the closest instance you're creating using new (or other instance creation techniques).

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

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.

Categories

Resources