Last active
April 13, 2016 18:06
-
-
Save jherax/9ac4d1602d30882f8cb0 to your computer and use it in GitHub Desktop.
JavaScript coercion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VALOR COERCIÓN | |
------- --------- | |
false false | |
0 false | |
“” false | |
NaN false | |
null false | |
undefined false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (u) { //u = undefined | |
var fn = function() {}, | |
o = { valueOf: function() { return 7 } }; | |
//coerción en diferentes tipos de objetos | |
write("a.", fn); //!!bool: true, type: function | |
write("b.", fn()); //!!bool: false, type: undefined | |
write("c.", u); //!!bool: false, type: undefined | |
write("d.", 0); //!!bool: false, type: number | |
write("e.", ""); //!!bool: false, type: string | |
write("f.", +""); //!!bool: false, type: number | |
write("g.", +"x"); //!!bool: false, type: number (NaN) | |
write("h.", null); //!!bool: false, type: object [Null] | |
write("i.", []); //!!bool: true, type: object [Array] | |
write("j.", {}); //!!bool: true, type: object [Object] | |
write("k.", o); //!!bool: true, type: object [Object] | |
write("l.", +o); //!!bool: true, type: number | |
function padRight (val, len) { | |
var c = " "; | |
len = len || 10; | |
return (val + new Array(len).join(c)).slice(0, len); | |
} | |
function write (nom, val) { | |
console.log(nom, "type:", padRight(typeof val), | |
"!!bool:", padRight(!!val, 7), "value:", val); | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
//accediendo elementos del array | |
var array = ["cero", "uno", "dos"]; | |
console.log("a.", array[+[]]); //coerción a number | |
console.log("b.", array[+!![]]); //coerción a number | |
console.log("c.", array['1']); //coerción a number | |
console.log("d.", array[ 2 ]); | |
//accediendo propiedades de un objeto | |
var obj = { id: 1, "5": "cinco" }; | |
console.log("e.", obj["5"]); //propiedad de obj | |
console.log("f.", obj[ 5 ]); //coerción a string | |
//coerción con números y strings | |
console.log("g.", 5 - '3'); // -> 2 | |
console.log("h.", 5 + '3'); // -> "53" | |
console.log("i.", 5 + (+'3')); // -> 8 | |
console.log("j.", 5 * '3'); // -> 15 | |
console.log("k.", "" - 3); // -> -3 | |
console.log("l.", true + 1); // -> 2 | |
console.log("m.", '1' + true); // -> 1true | |
console.log("n.", '1' - true); // -> 0 | |
console.log("o.", false - true); // -> -1 | |
//coerción con fechas | |
var d = new Date(); | |
console.log("p.", +d); //number, llama a .valueOf() | |
console.log("q.", d + ""); //string, llama a .toString() | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tomado del artículo: Coerción en JavaScript