I've seen code that goes along the line of
Object( existingObject ).myMethod();
Is this different than calling existingObject.myMethod() directly? More generally, what does Object(x) do?
The Object constructor creates an object wrapper for the given value.
If the value is null or undefined, it will create and return an empty
object, otherwise, it will return an object of a type that corresponds
to the given value. If the value is an object already, it will return
the value.
In your case, since the value is an object already, it will just return the value existingObject. So, no, it is not really different from calling existingObject.myMethod directly.
Documentation
Related
I am trying to make sense of this line of code:
const loanPeriod: number = get(product, 'TermMonths', this.defaultTerm) / this.monthsInAYear;
defaultTerm and monthsInAYear are global variables. product is an object and TermMonths is a number property of product. I don't know why the product & 'TermMonths' is needed. Can't you just divide the defaultTerm by monthsInAYear?
You can find the official documentation here, with an example. Using your case, the assumption is that product may or may not have a property called TermMonths. If it does, _.get will retrieve the value of that property. If it does not, the default, this.defaultTerm, is returned.
The _.get() method is used to get the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
Syntax:
_.get(object, path, [defaultValue])
Parameters: This method accepts three parameters as mentioned above and described below:
object: This parameter holds the object to query.
path: This parameter holds the path of the property to get. The path will be array or string.
defaultValue: This parameter holds the value returned for undefined resolved values.
Return Value: This method returns the resolved value.
src: https://www.geeksforgeeks.org/lodash-_-get-method/
In your case it gets the term of loan if not it takes default and divides it by months in years to get the total years.
I'm trying to do implicit property access with JavaScript. So, I'm trying to do something like:
const array = [];
if (array) { // --> here can it implicitly call .length?
// code
}
I was thinking I'd need to see the internals of if statement, but not sure if that's even possible.
The if statement coerces its argument to a boolean, just like the Boolean() function does.
As all objects are truthy, and arrays are objects, no intermediate primitive coercion needed (whose behavior could be overridden), so what you want is simply not possible.
You'll have to type out array.length.
I'm storing objects references inside my JavaScript Sets:
let set1 = new Set();
let obj = {};
Then if I use the has() method and the size property of the Set objects I get the next results:
console.log(set1.size) //it returns 1
console.log(set1.has(obj)) //it returns true
But, if I remove the reference of the object using the next code:
obj = null;
Then strange behaviour happens to me:
console.log(set1.size); //it returns 1 because they are Hard Sets.
console.log(set1.has(obj)); //it returns false
In the last line, why does it return false if the size is not changed? Then the reference is lost but the size is not changed.
In the last line, why does it return false if the size is not changed?
It returns size = 1 because it still holds one value (a reference to that object).
It returns false because it does not has a value that is equals to null.
But, if I remove the reference of the object using the next code:
You just overwrite a value of the variable. After that there is still one reference to the object left (inside the Set).
To remove an element from a Set you need to use Set.prototype.delete() method:
set1.delete(obj);
Presumably you need to do that while obj still holds a reference to the object.
When you override the value of obj:
obj = null;
You are only changing the value of obj, not of the Set entry, which still holds a reference to the object that was originally assigned to obj.
So the current state is:
obj: null
set1: [
{} // the object assigned to obj
]
So your line
console.log(set1.has(obj));
actually tests, if set1 contains null:
console.log(set1.has(null));
which is of course false (as it only contains the object).
To actually delete the object from the set, you can use:
set1.delete(obj);
You could also use a weakSet, if the reference in the set in only used as long as the reference to that object still exists somewhere else.
should a getter of a private property return a reference to the property or the value of the property (or a clone if the property is an object).
If the property has a primitive type (string, number, boolean) or a reference to an immutable object (String, Number), the answer is that you return the value/reference directly, since there's nothing the caller can do with it that will change your object's state.
If the property has a reference type to a mutable object (an array, an Object, a Date, lots and lots of other things), it depends on whether you want the caller to be able to change the properties on your object's copy of it. If you do, return the reference. If not, clone it.
For example, let's assume an object with a private array and some operations you can perform on that array:
var obj = (function() {
var o = {};
var theArray = [];
o.addItem = function(item) {
theArray.push(item);
};
o.getItemAt = function(index) {
return theArray[index];
};
return o;
})();
(Note that this is not meant to be a glorious example of object design. :-) )
Now you want to add a getArray operation. You can do that in two ways:
You can return a reference to the array:
o.getArray = function() {
return theArray;
};
...which means the caller can do all sorts of things to it (like remove items from it, an operation the object didn't previously support) that will change your object's state (because you've given them direct access to the formerly-private array).
Or you can return a clone of the array:
o.getArray = function() {
return theArray.slice(0);
};
...which means the caller can't do anything to the private array, they can only operate on the copy of it you gave them.
This depends whether the private property is immutable or not. If it's immutable, it can't be changed and there is no harm in returning it. If however it is mutable, then returning it will break the OOP principle of information hiding.
If it's a primitive immutable int/number, then returning the private property is fine since it achieves two things:
You hide the private property and provide an interface, and any changes to the implementation of the private property will not break the interface as easily
The returned object (in this case a value) can't be changed by the caller because it's a primitive type and is passed by value
If however, you return a mutable Array from a method and your language passes objects by reference, then you must accept the fact that the caller may decide to change this internal object - which could very easily lead to your internal code breaking.
For these reasons, if you plan to return a copy to a mutable object, return a copy and not the underlying object itself.
Generally wherever the property is to be used should not be able to change the property value. This satisfies the important principle of the OOP i.e Encapsulation.
I have a couple of these and think (know) that I'm doing something wrong (or could be simpler).
html:
<div class='item-to-select' data-global-id='55'>some</div>
var l=$(this).map(function(){
t=new Object();
t.global_id=$(this).data('global-id');
return t;
}).get();
var list=l[0]; // want to remove this
How would I remove this intermediary object? Or a better way
thx
If you mean that you don't want to have to define the l variable just so you can use it once in setting up your list variable you can do this:
var list = $(this).map(function(){
return {
global_id : $(this).data('global-id')
};
}).get()[0]; // note the [0] directly after .get()
The return from any function that returns an array (or array-like object) doesn't have to be assigned to a variable before you can use it. So:
var temp = someFuncReturnsArray();
console.log(temp[0]);
// can be replaced by
console.log(someFuncReturnsArray()[0]);
Of course if you need to do further processing on the returned array you need to put it in a variable. E.g., if you need to test its length, or if the function could possibly return null in some situations, etc. In the example above if an empty array was returned then obviously [0] will be undefined.
But if you only need the return value once you can just use it directly.
Note that I've removed the t variable from your code too. When creating an empty object it is considered good practice to say obj = {} rather than saying obj = new Object(). But you can create an object with properties in one step if the property values are already known. In the case of your function the t object you create isn't manipulated in any way other than adding a single property to it before you return it, so you can simply return an object literal directly instead of doing it in three steps.
The jQuery .get() method accepts an index.
So, you can write :
var list=$(this).map(function(){
t=new Object();
t.global_id=$(this).data('global-id');
return t;
}).get(0);