Retain function properties with javascript bind method - javascript

The javascript bind method returns a bound object however the returned object contains none of properties of the original object.
In case the property is set on the prototype object, I can use Object.setPrototypeOf to restore all the properties, however I can't figure out how to do the same for properties on the function itself.
var obj = function(){/*some code logic */}
obj.a = 1;
var boundedObj = obj.bind(obj);
boundedObj.a; //returns undefined
var boundedObj2 = Number.bind(Number);
boundedObj2.EPSILON// returns undefined
Object.setPrototypeOf(boundedObject2, Number);
boundedObj2.EPSILON // returns the epislon value

You can use Object.assign() to copy all the enumerable, own properties to the bound function.
var obj = function(){/*some code logic */}
obj.a = 1;
var boundedObj = obj.bind(obj);
Object.assign(boundedObj, obj)
console.log(boundedObj.a);

Related

JavaScript - using apply to invoke function, but 'this' is not being set to the object passed as first argument

I'm trying to use the apply method to invoke the objectify function, passing an array of values and using obj as the object. However, this is not being set to the object, but rather the global environment (window).
The purpose of this is to get an array of strings, then pass those strings to the function objectify. When the function is invoked, it takes the array values, splits them, and, if the object does not have a property with the string value, that property is created.
Here is the code below:
let obj = {};
let arr = ["user.name.firstname=John", "user.name.lastname=Smith"];
const objectify = x => {
let cur = this, v;
return x.split('.').forEach(e => /=/g.test(e) ?
(v = e.split('='), cur[v[0]] = v[1]) : cur[e] ?
cur = cur[e] : (cur[e] = {}, cur = cur[e]))};
objectify.apply(obj,arr);
The problem is this is set as Window rather than the object obj. How should I rewrite this code so that it sets obj as the this value?
The end result should be a modified obj object so it becomes:
obj = {user: {name: {firstname: 'John', lastname: 'Smith'}}};
This (no pun intended) is happening because objectify is an "arrow function" that always uses the this that it inherits from the lexical scope, and will ignore any passed via .apply or .call.
If you rewrite it as a normal function it should work as desired.

Object property in prototype

I have a problem I can't solved because I can't explain this behaviour :
var A = function(value) {
this.prop = value;
};
A.prototype = {
prop: 0
};
var a = new A(1);
var b = new A(2);
console.log(a.prop);
console.log(b.prop);
output :
1
2
But, with this code (almost the same) :
var A = function(value) {
this.prop.value = value;
};
A.prototype = {
prop: {
value: 0
}
};
var a = new A(1);
var b = new A(2);
console.log(a.prop.value);
console.log(b.prop.value);
I have this output :
2
2
Can anybody explain me this ?
Thanks...
EDIT :
Here's a solution :
var A = function(value) {
this.prop = {};
this.prop.value = value;
};
A.prototype = {
};
var a = new A(1);
var b = new A(2);
console.log(a.prop.value);
console.log(b.prop.value);
In example 1, this.prop is a primitive type, which is referenced by value and thus not shared between instances.
In example 2, this.prop is an Object whose reference is initialized from the prototype from a single object, so this single object is shared by all instances.
In the last "example" solution, you create a new object with = {}, so all instances now have their own object.
More details about primitive types : Primitive value vs Reference value
prototype is created only once and it's only a simple object whose childs are attached for all instances of a function it belongs to.
So it's basically the same as if you write:
var obj = { value: 0 };
var a = {}, b = {};
a.obj = obj;
b.obj = obj;
obj.value = 2;
as you can see both a.obj and b.obj references to the same obj and both a.obj.value and b.obj.value will be 2 in this example
This is happening because in JS, objects are passed by reference, while primitives are not.
Since the prototype is shared between the 2 instances, modifying a object on it will update all instances.
Think of a prototype property as a property shared amongst all instances.
Yet you can override it on each instance, that 's what you do in the first example.
Once overriden in each instance, you do not access any more to the prototype property with obj.prop, which now refers to the instance property. You would need to use obj.prototype.prop to read it again, but this syntax is illegal : you can use obj.__proto__.prop (non standard) or Object.getPrototypeOf(obj).prop (EcmaScript 5) to do so.
In the second instance, you do not change the property in the constructor : rather you change a property of this property. So both constructor access to the very same object, then change one of its property value, so the last to set it will 'win'. Here the prototype property is not overriden ('hidden') by the instance property and accessing obj.prop in fact access 'obj.prototype.prop'.

Reference behavior for property of an object

var myObject = new Object();
var myObjectCopy = myObject;
myObject.Name = 'alav';
// logs Name alav on both variable
console.log(myObject, myObjectCopy);
myObject = null;
// logs myObject as null and myObjectCopy still has name 'alav' -> bcoz of reference copy
console.log(myObject, myObjectCopy);
The same behavior is not getting replicated below.
var objA = {property: 'value'};
var pointer1 = objA;
// update the objA.property, and all references (pointer1 & pointer2) are updated
objA.property = pointer1.property;
objA.property= null;
// logs 'null null' because objA, pointer1 all reference the same object
console.log(objA.property, pointer1.property);
Why the above reference copy behavior is not applicable to inner properties(property here) of an object?
objA.property = pointer1.property; -> aren't reference COPY?
In case one you were setting reference to null therefore no change in actual object
myObject = null;// setting reference to null object , but no change in actual object
In second case you are making changes into the object(changing state of object)
by setting property to null
objA.property = null;
hence in every reference , value of property will be null

How can I store reference to a variable within an array?

I'm trying to create an array that maps strings to variables. It seems that the array stores the current value of the variable instead of storing a reference to the variable.
var name = "foo";
var array = [];
array["reference"] = name;
name = "bar";
// Still returns "foo" when I'd like it to return "bar."
array["reference"];
Is there a way to make the array refer to the variable?
Put an object into the array instead:
var name = {};
name.title = "foo";
var array = [];
array["reference"] = name;
name.title = "bar";
// now returns "bar"
array["reference"].title;
You can't.
JavaScript always pass by value. And everything is an object; var stores the pointer, hence it's pass by pointer's value.
If your name = "bar" is supposed to be inside a function, you'll need to pass in the whole array instead. The function will then need to change it using array["reference"] = "bar".
Btw, [] is an array literal. {} is an object literal.
That array["reference"] works because an Array is also an object, but array is meant to be accessed by 0-based index. You probably want to use {} instead.
And foo["bar"] is equivalent to foo.bar. The longer syntax is more useful if the key can be dynamic, e.g., foo[bar], not at all the same with foo.bar (or if you want to use a minimizer like Google's Closure Compiler).
Try pushing an object to the array instead and altering values within it.
var ar = [];
var obj = {value: 10};
ar[ar.length] = obj;
obj.value = 12;
alert(ar[0].value);
My solution to saving a reference is to pass a function instead:
If the variable you want to reference is called myTarget, then use:
myRef = function (newVal) {
if (newVal != undefined) myTarget = newVal;
return myTarget;
}
To read the value, use myRef();. To set the value, use myRef(<the value you want to set>);.
Helpfully, you can also assign this to an array element as well:
var myArray = [myRef];
Then use myArray[0]() to read and myArray[0](<new value>) to write.
Disclaimer: I've only tested this with a numerical target as that is my use case.
My solution to saving a reference is to pass a function instead:
If the variable you want to reference is called 'myTarget', then use:
myRef = function (newVal) {
if (newVal != undefined)
myTarget = newVal;
return myTarget;
}
To read the value, use myRef();. To set the value, use myRef(value_to_set);.
Helpfully, you can also assign this to an array element as well:
var myArray = [myRef];
Then use myArray0 to read and myArray[0](value_to_set) to write.
Disclaimer: I've only tested this with a numerical target as that is my use case.

What does the JavaScript "Object" function do?

What does the Object function in JavaScript do?
For example, what happens when we do Object(1)?
It forces something to be an object. I've not seen it being used in this way though.
var num = 1;
var obj = Object(num);
alert(typeof num); //displays "number"
alert(typeof obj): //displays "object"
alert(num + "," + obj); //displays "1,1"
The preferred, faster way to create an empty object on which you can put properties and methods on is by using {}. Three possible ways to create an object:
var emptyObj = {};
var emptyObj = new Object();
var emptyObj = new Object; // Object does not need an argument, so this is valid.
From the Mozilla developer site:
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 type that corresponds to the given value.
When called in a non-constructor context, Object behaves identically.
So Object(1) produces an object that behaves similarly to the primitive value 1, but with support for object features like assigning values to properties (Object(1).foo = 2 will work, (1).foo = 2 will not).
var obj = Object("test");
Creates a String "text", it's pretty similar to
var obj2 = "test";
Notice that the type of obj2 is "String" and of obj1 "Object"
Try this:
<script>
var obj = Object("test");
console.log(obj);
console.log(typeof(obj));
console.log(obj["0"]);
obj2 = "test";
console.log(obj2);
console.log(typeof(obj2));
console.log(obj2["0"]);
</script>
Creates an object http://www.w3schools.com/js/js_objects.asp
Object function is a constructor function, all other types(like Array, String, Number) inheritate it.

Categories