美文网首页重学JS
重学JS(三)— 运算符

重学JS(三)— 运算符

作者: 尹东方 | 来源:发表于2020-02-19 13:03 被阅读0次

1. 幂运算符**

console.log(2 ** 3); // 8

2. 正值运算符+

可以将非Number转为Number,与parseInt或者parseFloat不同

console.log(parseFloat('3abc'));    // 3
console.log(+'3abc');               // NaN
console.log(parseFloat('.3abc'));   // 0.3
console.log(+'.3');                 // 0.3
console.log(parseFloat(null));      // NaN
console.log(+null);                 // 0
console.log(parseFloat(undefined)); // NaN
console.log(+undefined);            // NaN
console.log(parseFloat(true));      // NaN
console.log(+true);                 // 1
console.log(parseFloat(false));     // NaN
console.log(+false);                // 0

3. 操作符in

如果所指定的属性确实存在于所指定的对象中,则会返回true,语法如下:

propNameOrNumber in objectName

下面的例子是 in 操作的常见用法。

// Arrays
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees;        // returns true
3 in trees;        // returns true
6 in trees;        // returns false
"bay" in trees;    // returns false (you must specify the index number, not the value at that index)
"length" in trees; // returns true (length is an Array property)

// Predefined objects
"PI" in Math;          // returns true
var myString = new String("coral");
"length" in myString;  // returns true

// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar;  // returns true
"model" in mycar; // returns true

4. 拓展语句...

展开对象或数组,可以在扩展数组和函数调用时使用。

// 扩展数组,不需要 concat
var parts = ['shoulder', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];

// 函数调用传参
function func(x, y, z) { }

var args = [0, 1, 2];
func(...args);

相关文章

  • 重学JS(三)— 运算符

    1. 幂运算符** 2. 正值运算符+ 可以将非Number转为Number,与parseInt或者parseFl...

  • Python小白学习进行时---js基础(2018-7-13)

    一、JS初识 二、JS语法 三、运算符 四、分支结构 ==============================...

  • 重学JS(三)—— promise

    Promise这东西,只会用,没有刻意去了解过。但有时不得不为它带来的便利感到惊叹。用的多了,对他的思想就会有一点...

  • JavaScript 02 (运算符和选择结构)

    js的关系运算符,js的逻辑运算符,js的赋值运算符,js的运算符的优先级问题,js的自增和自减,js的选择结构 ...

  • 运算符及js操作属性

    关系运算符 相等运算符 条件运算符 运算符的优先级 代码块 js操作属性 js操作style属性 js操作clas...

  • js操作属性 预解析 判断语句

    1. 三元运算符 2. 运算符的优先级 3. 代码块 4. js操作属性4.1 js操作style属性4.2 js...

  • 2019-01-25js基础语法

    一 什么是JS 二,js基础语法 三,变量 四, 运算符 五,分之结构

  • js运算符

    js基础语法:运算符,判断语句,数据类型,js对象 一、运算符 赋值运算符 =算数运算符 +-*/% ++...

  • 2020-03-16

    JavaScript 初识 《① JS 速览——进入 JS 的世界》[编号:js_01] 《② 运算符、运算符优先...

  • 2018-12-01

    赋值运算符 关系运算符 Unicode编码 相等运算符 条件运算符 运算符的优先级 代码块 js操作属性 js换肤...

网友评论

    本文标题:重学JS(三)— 运算符

    本文链接:https://www.haomeiwen.com/subject/zdqnfhtx.html