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.
Related
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
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);
This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 8 years ago.
I am not getting this into my head properly. I have seen a question which is related to it, but could not quite get anything out of it.
function Shane(){}
var sha = new Shane();
console.log(sha.__proto.__.__proto.__.__proto.__) //null
console.log(sha.constructor.prototype.constructor.prototype.
constructor.prototype)
//Shane [Can anyone explain me what5 is happening here]
Is constructor.prototype != .__proto.__?
Why do we have two ways to know the prototype chain?
__proto__ is the actual object used in the lookup chain to resolve methods.
prototype is the object used to create __proto__ with new.
Also, you have a typo, it should be .__proto__ not .__proto.__
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
What really answers the question is that if
var fn = function() { ... };
var obj = new fn();
then
obj.__proto__ === fn.prototype
however
fn.prototype.constructor == fn
if fn has no preset prototype (i.e. the initial value). This is according to the specification:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
So what you are observing is that there are objects such that
obj.__proto__ !== obj.constructor.prototype
Very subtle differnce from the equality far far above. :)
As for the second question: historical reasons + bad design.
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 3 years ago.
I was writing a script, and I had something like
var myObject = {};
if (myObject == {}){
...
}
I was surprised to find that this evaluated to false.
My own findings
Reading up on some of the funny querks javascript does, I found this article, which tells that {} will result in a "truthy" object in an if statement, for example
if ({}){ // true
...
}
Hm, well further ready discuesses String Equality, which for object comparison, says it will return true if they reference the same object. (Even {} === {} returns false)
The only reason I can think that is would return false is that the objects are technically at different memory address, and thus considered different objects.
Because every {} creates a unique object. You can't compare them that way.
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