The second scratch learning JavaScript-- (operator)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1. Numeric

In JS, data are double precision floating point , only the range of - ( 2^{53} - 1) to 2^{53} - 1between integer no exception. There are three types of digital symbol value: + infinity (positive infinity), - infinuty (minus infinity) and NaN (not-a-number non-numeric).

Constant Property:

var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

Digital methods:

Built mathematical object the Math: the Math provides an absolute value, the number of exponentiation, trigonometric functions, maximum, minimum, the random number, and square root operation function, a PI value.

console.log(Math.PI)  // 3.141592653589793
console.log(Math.abs(-100))
console.log(Math.log2(16))  // 4
console.log(Math.sqrt(16))  // 4
console.log(Math.random())  // 默认为(0, 1)
console.log(Math.random(10, 100))  // 测试发现结果仍然是(0, 1)的随机数

2. Operator

2.1 Arithmetic Operators

+ - * /% operators such as the python

console.log(1 / 2)  // 0.5自然除,没有向下取整
console.log(1 / 0)  // 无异常,返回无穷, Infinity
console.log(5 % 3)

console.log(parseInt(1 / 2))  // 0
console.log(parseInt(3 / 2))  // 1, 向下取整

// 下面三种与python中是一样的,round是四舍六入,五取偶
console.log(Math.floor( 3 / 2))
console.log(Math.ceil(3 / 2))
console.log(Math.round(3 / 2))
console.log(Math.round(1 / 2))

+ And -:

Unary operators, variable representing increment, decrement, i i ++ is to use, since further increase exhausted after 1, i ++ i is incremented by 1 from the first, using i (i Note that this case has added 1) The following is the test code:


let i = 0
let a = i++
console.log(a, i)  // 0, 1
console.log(a, i++)  // 0, 1注意i++是先使用再自增,所以打印出1,但是i已经变成2了

a = ++i  // i已经是2了,在++i, a为3, i为3
console.log(a, ++i)  // 3, 4注意++i是先自增在使用

// 单目运算符优先级高于双目运算符
i = 0
let b = ++i + i++ + i++ + i
console.log(b)  // 7

2.2 Comparison Operators

>, <,> =, <= No difference what is provided,! =, ==,! == ===

== equal loose, type conversion, equal == strict, no type conversion


console.log(100 > '200')  // flase
console.log(300 > '200')  // true
console.log(3000 > '2a')  // false,有疑问?

console.log('3000' > '2000')  // true

// 宽松比较
console.log(300 == '300') // true
console.log('200' == '200')

// 严格比较
console.log( 300 === '300')  // false
console.log('200' === '200')

From the above comparison, we found that when the comparison is implicit conversion to digital, using relatively loose when, as far as possible to ensure that the same type of comparison, otherwise it will cause implicit conversion, and implicit conversion rules are complex bad to control, if you do not know whether the same type, but is required by law to be equal, then you use === and! ==.

 

2.3 Comparison Operators

&&, || ,! AND, OR, NOT, these operators and other high-level languages ​​are supported by a short-circuit.

2.4 Operators

&, |, ^, ~, <<, >> bit and bit OR, XOR, inverted, left, right, and as python.

2.5 ternary operator

条件表达式?真值:假值 等价于简单的if ... else结构

if (条件表达式){

    表达式为真时执行的语句体
}
else{
    表达式为假时执行的语句体
}

2.6 comma operator

let n = 4 + 5, q = true, c = n > 10 ? '真' : '假'
console.log(n)
console.log(c)  // '假'

function test(){
    // return 2, n + q, c = n++
    return 5, c = n++, n + q  // 只能拿到最后的结果11
}

console.log(test())  // 9,只拿到了最后的值,现在还心存疑虑?让我们在测试一下
console.log(c)  // 9

2.7 Other

 


console.log('a' instanceof String)
console.log(10 instanceof(Number))  // 跟上面的写法不同,但是效果一样

w = new String('b')
console.log(w instanceof(String))  // true
console.log(new Number(20) instanceof Number)  // true
console.log(w instanceof Object)  // true, Object,其中Object是类型,所以O是大写字母

console.log(typeof 'w')
console.log(typeof('w'))
console.log(typeof(w))  // object,留意一下

instanceof required to use a variable type definition, is the object must be created with the new keyword statement , it can be used to determine the inheritance relationship. typeof return type is a string object.

delete delete objects, properties, array elements:

x = 42
var y = 43
let z = 60
myobj = new Number()

myobj.h = 4  // 创建了h属性
console.log(delete x)
console.log(delete y)
console.log(delete myobj.h)  // 可以删除用户自己定义的属性,不能删除系统内定义的属性,比如Math.PI
console.log(delete myobj)
console.log('==================')

var trees = new Array("reader", 'writer', 'apple', 'banana')
for(var i = 0; i < trees.length; i++)  // 注意i取不到trees.length
    console.log(trees[i])
console.log('==============')
delete trees[2]  // 注意数组中的元素被删除,但是空着的位置是undefined
for(var i = 0; i < trees.length; i++)
    console.log(trees[i])
/*
*==============
*reader
*writer
*undefined
*banana
*/

You can use the deletedelete various implicit declaration, but was varexcept statement

in determining the properties is within the object:

let people = new Array('Green', 'KD', 'Curry', 'Klay')
console.log(0 in people)  // true, 0在数组对象的index中
console.log(5 in people)  // false 5不在数组对象的index中
console.log('Curry' in people)  // false 'Curry'是值,不是属性
console.log('length' in people)  // true
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

// custom objects
let mycar = {
    color: 'black',
    brand: 'Porsche Cayenne',
    price: '¥1000000'
}

console.log('color' in mycar)  // true
console.log('model' in mycar)  // false
console.log('brand' in mycar)  // true

3. Expression

Basic expressions and almost python, python and analytical formula is also similar, but not recommended for use in non-standard ES6 in. Generator generator function is recommended, ES6 began to support.

function* inc(){
    let i = 0
    let j = 7
    while(true){
        yield i++
        if (!j--) return 100
    }
}

let gen = inc()
for(let i = 0; i < 10; i++)
    console.log(gen.next())
    
/*
*{ value: 0, done: false }
*{ value: 1, done: false }
*{ value: 2, done: false }
*{ value: 3, done: false }
*{ value: 4, done: false }
*{ value: 5, done: false }
*{ value: 6, done: false }
*{ value: 7, done: false }
*{ value: 100, done: true }
*{ value: undefined, done: true }
*/

 

Guess you like

Origin blog.csdn.net/sqsltr/article/details/94115710