操作符
基本的操作符允许你操作数值。
// Concatenation var foo = "hello"; var bar = "world"; console.log( foo + " " + bar ); // "hello world"
/ Multiplication and division 2 * 3; 2 / 3;
// Incrementing and decrementing // The pre-increment operator increments the operand before any further processing. var i = 1; console.log( ++i ); // 2 - because i was incremented before evaluation console.log( i ); // 2 // The post-increment operator increments the operand after processing it. var i = 1; console.log( i++ ); // 1 - because i was evaluated to 1 and _then_ incremented console.log( i ); // 2 - incremented after using it
作用于数字和字符串的操作符
在javascript中,字符和数字偶尔会有意想不到的行为
// Addition vs. Concatenation var foo = 1; var bar = "2"; console.log( foo + bar ); // 12
// Coercing a string to act as a number: var foo = 1; var bar = "2"; console.log( foo + Number(bar) ); // 3
当数字构造被当作函数调用时(如上例),它就会有把它的参数转换为数字的效果。单元加(+)操作符也可以做同样的事情。
// Forcing a string to act as a number (using the unary plus operator): console.log( foo + +bar ); // 3
作者:flyonok 发表于2013-5-7 22:33:02 原文链接
阅读:0 评论:0 查看评论