Copy an object in Javascript without using Jquery [duplicate] - javascript

This question already has answers here:
How do I correctly clone a JavaScript object?
(81 answers)
Closed 7 years ago.
I want to create a copy of a JavaScript object which has enumerable and non-enumerable properties. I want make a exact replica of the object with all enumerable and non-enumerable properties copied to the new one.
Any help how can it be done?

JSON.parse(JSON.stringify(obj))

You should use Object.create or it's backward compatible counterpart.
if(!Object.create){
Object.create = function(o){
function F(){}; F.prototype = o;
return new F;
}
}
var oldObj = {prop1:'val1', prop2:'val2', prop3:'val3'};
var newObj = Object.create(oldObj);
delete newObj.prop2;
console.log(newObj); console.log(oldObj);

Related

Can Array.isArray be tricked? [duplicate]

This question already has answers here:
Can you fake out Array.isArray() with a user-defined object?
(2 answers)
Closed 1 year ago.
I understand the concept of prototypes very well; so when I tried this, I was confused at first:
var obj = {};
Object.setPrototypeOf(obj, Array.prototype);
console.log(Array.isArray(obj)); // false?
Even scarier:
var arr = [];
Object.setPrototypeOf(arr, Object.prototype);
console.log(Array.isArray(arr)); // true?? 8-{
Until I did my research and found that Array.isArray looks for a hidden property of the object: [[DefineOwnProperty]].
Is this something that is completely immutable on an existing object? Or could a plain object {} be mutated such that calling Array.isArray on it would return true?
No, Array.isArray cannot be tricked.
And there's no way to change an object between an array and non-array. Just like you cannot change the callability of a function object, not even through the target of a proxy object.

Where does the Array.prototype functions come from? [duplicate]

This question already has answers here:
Why does Array.prototype return an empty array?
(2 answers)
Closed 6 years ago.
Since Array.prototype itself returns an empty array, where do all functions come from?
Why I can call functions something like Array.prototype.sort, even Array.prototype is not object?
Array.prototype.__proto__ is an object.
I have same question on the case of Function.prototype.
Array.prototype is an object, and most functions you used for array come from it. So it's the prototype.
Open chrome dev bar, and check it like the below image.
even Array.prototype is not object?
How did you get that idea? It is an object:
typeof Array.prototype // "object"
As such it can have properties like every other object. And it does:
Array.prototype.hasOwnProperty("sort") // true
Array.prototype.sort // function(compare) { … }
This is the same for every other array, you can create custom properties on them like on every javascript object:
var foo = [];
foo.bar = "baz";
typeof foo.bar // "string"
Array.isArray(foo) // true

merge two JavaScript objects with priority [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 6 years ago.
I have two JavaScript objects like
let master = {A:0, B:2};
let slave = {B:1, C:1};
I need:
result == {A:0, B:2, C:1};
Does JS have a simple command to merge in that way?
Use Object.assign
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
let master = {A:0, B:2};
let slave = {B:1, C:1};
console.log(Object.assign(slave, master));

Translating literal object into object's properties [duplicate]

This question already has answers here:
How to duplicate object properties in another object? [duplicate]
(16 answers)
Closed 8 years ago.
Given this object:
var myObject = {name: 'David', date: '11/13/2014'};
I want my constructor to set its object properties based on myObject:
function myClass(_object){
// set given object's properties here
}
I want to set the properties through a loop, so I wont have to set every property by hand - because the object will be dynamic.
I'm looking for a cross-browser solution.
Perhaps what you want is this:
function MyClass(_object) {
for (var p in _object) {
if (_object.hasOwnProperty(p)) {
this[p] = _object[p];
}
}
}
Don't forget to use new:
var obj = new MyClass(myObject);
Fiddle

Javascript : Check if object has properties [duplicate]

This question already has answers here:
if (key in object) or if(object.hasOwnProperty(key)
(9 answers)
Closed 8 years ago.
There are several answers here how to check if a property exists in an object.
I was always using
if(myObj.hasOwnProperty('propName'))
but I wonder if there is any difference from
if('propName' in myObj){
They are almost equal, the difference is that hasOwnProperty does not check down the prototype chain, while in does.
An example
var test = function() {}
test.prototype.newProp = function() {}
var instance = new test();
instance.hasOwnProperty('newProp'); // false
'newProp' in instance // true
FIDDLE
As noted, Object.hasOwnProperty only returns "own properties", i.e. properties that are added directly, and not properties added to the prototype.
Yes, there is difference. hasOwnProperty() ignores properties and methods which are added with prototype. I try to explain with examples. For instance if you have prototype of object
Object.prototype.something = function() {};
And let's say you have following object
var obj = {
"a" : "one",
"b" : "two"
};
And loop:
for ( var i in obj ) {
//if (obj.hasOwnProperty(i)) {
console.log(obj[i]);
//}
}
Without hasOwnProperty it will output one two function(), while with hasOwnProperty() method only one two
See the differences between First and Second DEMOS

Categories