基本包装类型:
指基本类型的数据变量通过调用属性或者方法包装成了复杂类型,这个变量也称为基本包装类型对象
<script>var str = 'hello';str = str.replace('ll', 'ee');console.log(str); //heeeo//通过基本包装,无论Boolean对象中传入true还是false,在逻辑与中其结果都是&&后面的值:var flag = new Boolean(false)var result = flag && true;console.log(result); //truevar flag1 = new Boolean(true)var result1 = flag1 && true;console.log(result1); //truevar flag2 = new Boolean(true)var result2 = flag2 && false;console.log(result2); //false</script>
操作符
指一些符号,用来做运算的,具体如下:
算数运算符:
+、- 、 * 、/ 、%
<script>//算数运算:console.log(111 + 222); //333console.log(111 - 222); //-111console.log(111 * 222); //24642console.log(111 / 222); //0.5console.log(5 % 3); //2var num = 10;var sum = (5 + num)*2;console.log(sum);//30</script>
递增递减运算符(一元运算符):
++ 、–(前置++(–)和后置++(–)的区别:前置++或–是先参与运算,之后在自身+1或-1;后置++或–是先自身+1或-1,之后在参与运算)、delete删除一个对象的属性或者一个数组中某一个键值、void 运算符(不常用)、in关系运算符(判断某个变量是否在某数据中)、
<script>//也可以叫一元运算符:var num = 10;console.log(num++); //10console.log(num); //11console.log(++num); //12console.log(num); //12var num2 = 10;console.log(++num2); //11console.log(num2); //11console.log(num2++); //11console.log(num2); //12//后置++:先参与运算,运算完后自身在+1(后置--同理)//前置++:先自身+1,然后再参与运算(前置--同理)var num = 10;var sum = ++num + 10;console.log(sum); //21console.log(num); //11var num = 10;var sum = num++ + 10;console.log(sum); //20console.log(num); //11var num = 10;var sum = num++ + ++num; //特别注意,当num自己每运算一次,自己的值就会改变,因此:10 + 12 = 22console.log(sum); //22console.log(num); //12var num = 10;var sum = ++num + num++; //11 + 11 = 22console.log(sum); //22console.log(num); //12var num = 10;var sum = ++num + ++num; //11 + 12 = 23console.log(sum); //23console.log(num); //12var num = 10;var sum = num++ + num++; //10 + 11 = 21console.log(sum); //21console.log(num); //12//delete删除数组元素或对象的属性:var x = 3,arr = [1, 2],obj = {naem: 'jack'};delete x;console.log(x); //3,不能删除除数组和对象的其他数据类型的数据delete arr[0];console.log(arr[0]); //undefinedconsole.log(arr[1]); //2delete obj[name];console.log(obj.name); //undefined// 逻辑中断或短路引起代码不执行情况:var num1 = 10;var result1 = false && ++num1;var result2 = true || num1++;console.log(num1); //10//void(表达式),第一个案例不会被跳转,第二个案例会被跳转:<a href="javascript:void(0)">点击</a><a href="javascript:void(document.form.submit())">点击</a>//判断属性是否在对象中:var obj = {name: 'jack'};if ('name' in obj) {console.log('name属性存在obj中'); //name属性存在obj中,注意属性是字符串};</script>
赋值运算符:
+=、-=、*=、/=、%=、**=、<<=(左移位赋值,<<为左移位运算)
<script>var num1 = 10;num1 += 1;console.log(num1); //11var num2 = 10;num2 -= 2;console.log(num2); //8var num3 = 10;num3 *= 2;console.log(num3); //20var num4 = 10;num4 /= 2;console.log(num4); //5var num5 = 10;num5 %= 7;console.log(num5); //3var num = 2;num **= 3;console.log(num);//8var a = 2;var b = 3;a <<= b; //a = a << b;console.log(a); //16</script>
比较运算符:
/>、<、>=、<=、== 、 ===、 != 、 !==
<script>console.log(10 > 5); //truwconsole.log(10 < 5); //falseconsole.log(10 >= 5); //truwconsole.log(10 <= 5); //falseconsole.log('5' == 5); //trueconsole.log('5' === 5); //falseconsole.log('5' != 5); //falseconsole.log('5' !== 5); //true</script>
逻辑运算符:
&&–与,并且、||–或,或者、!–非,取反(逻辑中断情况:当&&前面的表达式是非 时,后面的表达式不再执行,当||前面的表达式为真时,后面的表达式不再执行),扩展二进制逻辑运算符:&(按位与)、~(按位非)、|(按位或)、^(按位异或)
<script>//逻辑运算:console.log(false&&false);//falseconsole.log(true&&true);//trueconsole.log(false&&true);//falseconsole.log(true&&false);//falseconsole.log(false||false);//falseconsole.log(true||true);//trueconsole.log(false||true);//trueconsole.log(true||false);//trueconsole.log(111 && 222); //222console.log(111 || 222); //111console.log(111 != 222); //trueconsole.log(111 > 222 && true); //falseconsole.log(111 < 222 && false); //falseconsole.log(111 > 222 || true); //trueconsole.log(111 < 222 || false); //truevar num1 = 10;var num2 = 20;console.log(num1=num2&&5>6);//falseconsole.log(num2>10||5<0);//truevar num1 = 10;var sum = (num1+10)*5;console.log(sum);//100var result = (4 >= 6 || '人' != '狗' && !(12 * 2 == 144) && true);console.log(result);//truevar num = 10;var result2 = (5 == num / 2 &&(2 + 2*num).toString() === '22');console.log(result2);//true</script>
运算符优先级:
提示:本文图片等素材来源于网络,若有侵权,请发邮件至邮箱:810665436@qq.com联系笔者 删除。
笔者:苦海