var b = true;
b.foo = 'whatever'; // Auto-boxing occurs?
b.foo; // undefined - why?
Can I retrieve the value of property foo now?
var b is initially set to Boolean value. to assign dot notation to a variable, it has to be a javascript Object.
if b is set as var b = {}, b.foo = 'whatever'; should work.
For better practice always check type of variable before switching its datatype:
var b = true;
if(typeof b === 'object'){
b.foo = 'whatever';
}
Related
I need helps with convert the statement below to ES5 syntax. What will it be in ES5?
const { a, b, c = “foo” } = this.props;
I suggest to use an explicit check if property c exists in the given object or not. If not given, then use the default value.
var a = this.props.a,
b = this.props.b,
c = this.props.c === undefined ? 'foo' : this.props.c;
The otherwise used pattern
c = this.props.c || 'foo';
does not work for given falsy value like zero.
Why do you need a check with undefined (kudos to loganfsmyth for mention this problem in comments)?
Because undefined is the value for the check for default parameters in a function in ES6.
const f = (c = 'foo') => console.log(c);
f(); // 'foo'
f(undefined); // 'foo'
f(0) // 0
var
a=this.props.a,
b=this.props.b,
c=this.props.c||"foo";
Object destructuring trys to find properties with the same name in the object, so
{prop}=obj
equals:
prop=obj.prop;
The default parameter can be easily achieved with an Or operator:
prop=obj.prop || default;
or if you want to count falsys as a prop, itll be:
prop=("prop" in obj)?obj["prop"]:default;
I believe it would be :
var a = this.props.a,
b = this.props.b,
c = this.props.c || "foo"
At least I hope, otherwise it'll be raining downvotes
The easiest way to achieve this would be:
var a = this.props.a,
b = this.props.b,
c = this.props.c===undefined?"foo":this.props.c
But the better way would be to just use the this.props object instead of storing it in local variables.
In javascript there is this below given code.
A.B.C.D.someVariable= null;
I need to understand how to write this code in typescript ?
You can do
A.B.C.D.someVariable = null;
However in that case, someVariable won't be just yet another variable. It'll be considered as property for Object D which is a property of Object C which is a property of Object B which is a property of object A.
So you could do (in a very simplified term)
var A = { };
A.B = {};
A.B.C = {};
A.B.C.D = {};
A.B.C.D.someVariable = null;
Having the code:
foo = {};
foo.bar = 78;
foo.bar.baz = null;
testing foo.bar.baz against null:
if( foo.bar.baz === null )
console.log("foo.bar.baz is null");
else
console.log("foo.bar.baz is NOT null");
results in "foo.bar.baz is NOT null". Why isn't it null since i set it up explicitly?
Because primitives don't have properties, and the value in foo.bar is a primitive.
When you access (get or set) a property on a primitive, the JavaScript engine creates an object for that primitive, sets or retrieves the property value (if any), and then throws away the object. (That's why (42).toString() works; the primitive is promoted to an object backed by Number.prototype, toString is retrieved from that object and called with this referring to the object, and then the object is thrown away.)
So although an object was created in your foo.bar.baz = null statement, and its baz property was set to null, that object was never stored anywhere (certainly not in foo.bar), and so it got thrown away. Later when you do if (foo.bar.baz === null), a new object, which doesn't have the property, is created and you get undefined for its baz property (because it doesn't have one). (Naturally, JavaScript engines can optimize this process to avoid unnecessary object creation.)
We could create a function on Number.prototype that returned the object that gets created, to demonstrate that the object creation really does happen each time you access a property on a primitive:
// Add a `nifty` method to numbers
Object.defineProperty(
Number.prototype,
"nifty",
{
value: function() {
console.log("Number#nifty called");
return this;
}
}
);
var n = 42; // A primitive number
console.log(typeof n); // "number"
var obj1 = n.nifty(); // Creates an object, which we keep
console.log(typeof obj1); // "object"
var obj2 = n.nifty(); // Do it again, and a new object is created
console.log(obj1 === obj2); // false, they're not the same object
If you want to set properties on a number and keep them, you can do that by explicitly creating a Number object:
var n = new Number(42);
n.baz = null;
console.log(n.baz === null); // true
It's rare to want to do that, but it's possible. Just beware that as we showed earlier, two Number objects with the same raw value are not == to each other:
var n1 = new Number(42);
var n2 = new Number(42);
console.log(n1 == n2); // false
console.log(n1 === n2); // false
>, <, >=, and <= will coerce the number back to a primitive and use the raw value, but == and === will not.
Working with numeric value as object in strict mode will run into exception.
By default foo.bar.baz = null will be undefined because foo.bar is not object and .baz will not be set.
This is Your code in strict mode:
'use strict';
var foo = {};
foo.bar = 78;
foo.bar.baz = null;
if(foo.bar.baz === null )
alert("foo.bar.baz is null");
else
alert("foo.bar.baz is NOT null");
here is solution:
'use strict';
var foo = {};
foo.bar = {};
foo.bar.num = 78; // your number here
foo.bar.baz = null;
if( foo.bar.baz === null )
alert("foo.bar.baz is null");
else
alert("foo.bar.baz is NOT null");
p.s. always put 'use strict' inside Your JS files to be able to make JS less versatile.
This is because
foo = {};
foo.bar = 78;
foo.bar.baz = null;
console.log(foo.bar.baz); // undefined
since you are trying to set a property of a number.
foo.bar is a number primitive, not an object. You can't set properties on a number primitive, but you can set properties on a number object.
foo = {};
foo.bar = new Number(78);
foo.bar.baz = null;
Now foo.bar.baz == null.
I found this example in a book:
// Create _callbacks object, unless it already exists
var calls = this._callbacks || (this._callbacks = {});
I simplified it so that I did not have to use a special object scope:
var a = b || (b = "Hello!");
When b is defined, it works. When b is not defined, it does not work and throws a ReferenceError.
ReferenceError: b is not defined
Did I do anything wrong? Thank you!
When performing a property lookup like this._callback, if the _callbacks property does not exist for this you will get undefined. However if you just do a lookup on a bare name like b, you will get a reference error if b does not exist.
One option here is to use a ternary with the typeof operator, which will return "undefined" if the operand is a variable that has not been defined. For example:
var a = typeof b !== "undefined" ? b : (b = "Hello!");
It should work in this form:
var b, a = b || (b = "Hello!", b);
// ^ assign b
// ^ () and , for continuation
// ^ return the new value of b
//=> result: a === b = "Hello!"
Like how you can have:
var obj = {};
obj.a = obj.b = 5;
obj.a === obj.b === 5; //just imagine that it won't evaluate to true === 5
Is there the same thing for object literals? Something along the line of:
var obj = {
a : b : 5
};
obj.a === obj.b === 5; //just imagine that it won't evaluate to true === 5
Is there the same thing for object literals?
No, there isn't. Although you can use any other expression on the right-hand side of the :, including an assignment expression, you can't use another property initialization expression. And you can't use an assignment expression instead (assigning to one of that object's properties) because the object hasn't been assigned to the target variable yet, so you have no way of referencing it.