I know very well the difference b/w the 2 operators in context to primitive types (number, string, boolean, null and undefined). I mean all that stuff,
0 == false // true
0 === false // false, because they are of a different type
1 == "1" // true, auto type coercion
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
Consider the snippet,
var str3 = new String("abc");
var str4 = new String("abc");
// false as 2 different objects, referential equality just like in java
alert(str3 == str4);
var str5 = new String("abc");
var str6 = str5;
alert(str6 == str5); // true, as pointing to the same object
For str5 and str6, they will compare truthfully. Use of the strict comparison (===) will, of course, produce the same result, as they are the same value and type; indeed, they are the same object!
Now consider,
function Person(name) {
this.name = name;
}
// the function person has a prototype property
// we can add properties to this function prototype
Person.prototype.kind = ‘person’
// when we create a new object using new
var zack = new Person(‘Zack’);
// the prototype of the new object points to person.prototype
zack.__proto__ == Person.prototype //=> true
zack.__proto__ === Person.prototype //=> false
I find myself really confused with the last 2 lines:
zack.__proto__ == Person.prototype //=> true
zack.__proto__ === Person.prototype //=> false
As per my understanding zack.__proto__ and Person.prototype are pointing to the same object (same location in memory), hence true.
If so, why zack.__proto__ === Person.prototype false then, as the type of both zack.__proto__ and Person.prototype is object and since they are pointing to the same location in memory, they must have equal values.
Foodefined?'or"instead of‘.true, as expected.__proto__property is non–standard and deprecated, so you shouldn't be using it anyway. Try Object.getPrototypeOf instead.