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)
Closed 8 years ago.
I stumbled with the following JavaScript code:
var Employee = function (entity) {
var employee = this;
entity = entity || {};
employee.employeeId = entity.EmployeeId;
employee.email = entity.Email;
employee.firstName = entity.FirstName;
employee.lastName = entity.LastName; // ....
But I couldn't understand the following sentence:
entity = entity || {};
|| is the OR statement in JavaScript. Your function receives entity as variable. When entity is null or undefined, your function will fill it up with empty object which is the same as {}.
Related
This question already has answers here:
Why can't a property be added to a null value?
(2 answers)
Closed 6 years ago.
property myProperty is not assigned to variable foo, foo is an object.
var foo=null;//null is an object
foo.myProperty = "my value";
console.log(typeof foo.myProperty);
A Javascript object should be declared like this
var myObject = {};
Try to modify you code like
var foo = {};
foo.myProperty = "a string";
console.log(typeof foo.myProperty);
This question already has answers here:
What does the following code mean in javascript? [duplicate]
(2 answers)
Closed 6 years ago.
javaScript(variable)
var s = s || {};
s.c = {};
what purposes it will be use?
var s = s || {};
This means that if s is null, undefined or false (it computes to false), then an empty object {} will be assigned to s, so that the second line would not cause an error.
But this notation is inacurate. It should be something like:
var s = (typeof s == 'object') ? s : {};
because in the first example if s is a number the second line will still cause an error.
In the second example notation A ? B : C; is equal to:
if(A){
B;
}else{
C;
}
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I was trying to "put a function into an object" so I wanted to do something like this but I'm getting errors everywhere.
var someobject = {
makename(1) : null,
makename(2) : null,
makename(3) : null,
makename(4) : null
};
function makename(num) {
return (identifier + ' Bot' + num)
}
In modern (ES2015) JavaScript environments, you can do this:
var someobject = {
[makename(1)]: "foo",
[makename(2)]: "bar"
};
The [ ] wrapper around the property name allows it to be an arbitrary expression. The result of evaluating the expression is interpreted as a string and used as the property name.
var someobject = {}
someObject[makename(1)] = null;
someObject[makename(2)] = null;
someObject[makename(3)] = null;
someObject[makename(4)] = null;
This works everywhere. However, #pointy's solution is nicer!
This question already has answers here:
create object using variables for property name [duplicate]
(4 answers)
Closed 8 years ago.
I have
object.name = 'foo';
var value = 'bar';
...
var params = { object.name : 1, value : value};
I want a result equivalent to
var params = { foo : 1, value : bar };
what can I do?
Objects are sometimes called associative arrays, since each property is associated with a `string value that can be used to access it.
Try using [] to set object property -
object.name = 'foo';
var value = 'bar';
var params = { value:value};
params[ object.name] = 1;
Output:- {value: "bar", foo: 1}
This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 8 years ago.
I'm trying to find a way of using a string to refer to a variable.
Here is an example using jQuery:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return foo[1];
});
I want this to return the number '2' -
but as foo is a string it will return the sub string 'r'.
edit - the answer I was looking for was:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return eval(foo)[1];
});
I don't know if this is quite what you mean, but a Javascript object can do this.:
foo = {}; // create object
foo["string"] = [1,2,3]; // now the list [1,2,3] can be referenced
// by foo.string, or foo["string"]
console.log(foo["string"][1]); // Output with string.
Is that what you mean?