Address this question: https://github.com/YvetteLau/Step-By-Step/issues/9
== operator will implicit conversion.
- Object
== operator attempts by the method valueOf and toString convert the object to its original value (a value of type string or numeric).
const a = {
i: 1,
// valueOf 也可达到相同效果
toString: function () {
return a.i++;
}
}
a == 1 && a == 2 && a == 3; // true
- Array
for arrays of objects, toString method returns a string from the toString each element in the array () Returns the value by calling join () method connector (separated by a comma) components.
var a = [1,2,3];
a.join = a.shift;
a == 1 && a == 2 && a == 3; // true
- Symbol
when Symbol object is converted to values of the original type, toPrimitive method calls, returns the original value of the corresponding object type.
let a = {
[Symbol.toPrimitive]: ((i) => () => ++i) (0)
};
a == 1 && a == 2 && a == 3; // true
- Modify the get method window
var val = 0;
Object.defineProperty(window, 'a', {
get: function() {
return ++val;
}
});
a == 1 && a == 2 && a == 3; // ture
- With the use of keywords
emmm ... with keywords like rarely used
var i = 0;
with({
get a() {
return ++i;
}
}) {
a == 1 && a == 2 && a == 3; // true
}
reference:
Reproduced in: https: //www.jianshu.com/p/a74223e37e3b