Javascript基础 - js中曾经忽略的知识点

本文总结了一些javascript开发中容易忽略或混淆的知识点.

1. parseInt(string, [radix]),parseFloat(string)

一般我们省略第二个参数,parseInt(‘100’) === 100,此时默认认为‘100’是一个十进制数的字符串。

parseInt(‘100’,10),100

parseInt(‘100’,2),4

parseInt(‘-FF’,10),-255

parseFloat(‘3.0’),3//小数位为0得到3

parseFloat(‘3.0001sss’),//3.0001 从0位开始截取有效数字字符串

 

2. typeof null,null instanceof Object

typeof null;//’object’,null是一个原生非可调用对象

null instanceof Object;//false,可以理解null是一个空对象(对象占位符),而不是一个实例

typeof undefined === ‘undefined’

typeof null === ‘object’

a = {b:1}, typeof a === ‘object’ //a为一个引用类型

typeof 9 === ‘number’

typeof ‘seed’ === ‘string’

typeof false === ‘boolean’

js的原始类型包括:Number,Boolean,Null,String,Undefined

 

3. [1,2,3].reduce(function(pre,current){return pre*current},2) === 12

Reduce方法接收一个接收previousValue和nextValue,返回当次计算结果回调方法,还有一个初始参数

 

4. 3+(3===3)?'some':'any' === ‘some’

+运算符的优先级高于?:,所以3+(3===3) === 4,所以输出’some’

 

5. var name = 'World!';

    (function () {

     if (typeof name === 'undefined') {

        var name = 'Jack';

        console.log('Goodbye ' + name);

      } else {

        console.log('Hello ' + name);

      }

})();

输出’Goodby Jack’,当注释掉var name = 'Jack';则输出’Hello World!’

 

6. arr=[1,2,3];arr[10]=0;

arr.filter(function(number){return number === undefined});//[]

此时

arr :[1, 2, 3, undefined × 7, 0];

arr.length === 11;

filter方法遍历的是arr的key,我们可以通过

arr.hasOwnProperty('1') === true

arr.hasOwnProperty('10') === true

arr.hasOwnProperty('6) === false

所以filter方法其实只会遍历到’0’,’1’,’2’,’10’这四个key,而3-9这6个key不存在,当然arr['6'] === undefined是成立的。所以数组中key的缺失和为一个key赋值为undefined也是不一样的。Array的map,forEach也是根据index key是否初始化来判断是否进行操作。

 

使用如下方法克隆arr

arr2 = JSON.parse(JSON.stringify(arr))

得到的arr2为:[1, 2, 3, null, null, null, null, null, null, null, 1]

可见stringify方法在遍历数组时发现缺失的key用null做了默认值。

 

7. 0.2 – 0.1 === 0.1

But

0.3 – 0.2 === 0.09999999999999998

0.8 - 0.6 === 0.20000000000000007

通过这三组计算我们发现js没有准确的数字,尤其是减法,但我们可以发现如果用toFixed方法就可以四舍五入的到固定位数的浮点数

(0.8-0.6).toFixed(2) === ‘0.20’

(0.8-0.6).toFixed(1) === ’0.2’ == 0.2

(0.8-0.6).toFixed(1) == 0.2

再使用parseFloat((0.8-0.6).toFixed(1)) === 0.2

 

8. 这里还是数字的坑,parseInt(233.001,0)相当于parseInt(233.001,10),parseInt(233.001),parseInt(1000.22,false),

parseInt(1000.22,undefined),parseInt(1000.22,null)

parseInt的第二个参数为0,undefined,null,false,10,缺失,都认为是十进制

 

9. [1]==[1] === false

对象比较的是地址,等号前后的[1]分别是不同的对象,引用不同,所以不相等

B=[2,3,4];C=B

则B==C  === true, B===C  === true

[2,3,4]==[2,3,4] ===false

 

10.  a=[1,3],b=[1,5],c=[1,5]

b == c //false

b === c //false

a < b//true

每个数组对象是不一样的实例,用 == 或 === 比较的时候,其实比较的是对象引用,所以b === c //false,但对于大于小于,会按照数组从前往后进行比较。

其实不止数组,对于按数字索引的对象如:String也是如此

m = new String('sdfg');

n = new String('sdfz');

p = new String('sdfz');

q=new String(‘a’);

所以p == n //false

p>n//false

p<n//false

p>q//true

m<n//true

 

11.  Prototype,__prpto__,Object.getPrototypeOf

function User = function(params){

this.name = params.name;

this.id = params.id;

}

 

User.prototype.greet = function(){

alert(‘Hi,I am ’ + this.name + ‘!’);

}

User.ptototype //Object{},得到的是 User的实例的prototype

Object.getPrototypeOf(new User())//Object{}, getPrototypeOf是获取一个实例的prototype的标准方法

Object.getPrototypeOf(User)//function(){},js中除了原始数据类型,都是对象,function User,就是Function的一个对象实例

User instanceof Function === true

__ptoto__ 是获取实例的prototype的非标准方法

 

12.  "1 2 3".replace(/\d/g, function(a,b){

console.log(a);console.log(b);

return a%2;

}) === ‘1 0 1’

String.prototype.replace接收一个正则和一个回调,每次调用回调函数会传递[Item,Index]这样的两个参数,

 

13.  eval(null) === ‘null’, eval(undefined) === ‘undefined’

eval('var v=12,b="12"'),将申明v和b两个变量

 

部分内容参考自:http://ourjs.com/detail/52fb82e13bd19c4814000001

 

posted @ 2017-03-03 12:54  王仲春  阅读(267)  评论(0编辑  收藏  举报