Points to note in JavaScript standard objects

In the JavaScript world, everything is an object.

To summarize, there are a few rules to follow:

  • Do not use new Number(), new Boolean(), to new String()create wrapper objects;

  • Use parseInt()or parseFloat()to convert any type to number;

  • Used toString() convert any type to string, or directly call a toString()method of an object;

  • Usually it is not necessary to convert any type to booleanre-judgment, because it can be written directly if (myVar) {...};

  • typeofThe operator can determine number, boolean, string, functionand undefined;

  • judge Arrayto use Array.isArray(arr);

  • Judge nullplease use myVar === null;

  • Determine whether a global variable exists or not typeof window.myVar === 'undefined';

  • Inside the function, it is used to determine whether a variable exists typeof myVar === 'undefined'.

Finally, does any object have toString()methods? nulland undefinedthere is no! It does, except for these two special values, albeit nulldisguised as objecttypes.

Also pointed out that the numberobject call toString()reported SyntaxError:

123.toString(); // SyntaxError

In this case, special treatment is required:

123..toString(); // '123', 注意是两个点!
(123).toString(); // '123'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325589431&siteId=291194637