博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Some useful JS techniques that you may not know
阅读量:6004 次
发布时间:2019-06-20

本文共 2783 字,大约阅读时间需要 9 分钟。

I complete reading recently. The book is more about basics in JavaScript and suitable for beginners. Here are a list of my benefits and extensions from the book.

Math

JavaScript developers often capitalize the constructor name to distinguish the constructors from normal functions. Therefore, some developers may mistake Math as function since the name is capitalized while Math is really just an object.

console.log(typeof Math); // objectconsole.log(typeof Array); // function

Function

There are two ways to construct a function:

  • Function declaration/expression
function sum(x, y) {    return x + y;}console.log(sum(3,4)); //7
  • constructor
let multiply = new Function("x", "y", "return x * y;");console.log(multiply(3,4)); //12

In development, we often need to use or to switch the function context. E.g.

function print(){    console.log(this.a);}print.call({a: 'hello'}); //hello

Some interview questions will ask why print doesn't define call as its property but print.call() won't throw error. It's because print is an instance from Function constructor so it inherits all the methods defined in Function.prototype through .

print.call === Function.prototype.call

How to do Array check

can be used to determine the types for primitive datatypes. But it won't be able to distinguish between arrays and objects.

console.log(typeof [1,2]); //objectconsole.log(typeof {}); //object

There are several wasy to do Array check:

  • API:
console.log(Array.isArray([1,2])); //true
console.log(Object.prototype.toString.call([1,2])            .toLowerCase() === '[object array]'); //true

Note here we have to use with to switch the calling context, as is overriden.

console.log(Object.prototype.toString.call([1,2])); //[object Array]console.log([1,2].toString()); //1,2
[1,2] instanceof Array === true;

won't work for custom datatypes. In this case we can use to determine the type.

class Person {}let mike = new Person();console.log(Object.prototype.toString.call(mike)); // [object Object]console.log(mike instanceof Person); // true

undefined vs null

undefined - no value

There are two cases where a variable is undefined.

  • The variable is deifned but not assigned any value.
let s;console.log(s); //undefined
  • The variable is NOT defined at all.
let obj1 = {};console.log(obj1.a); //undefined

null - special value

let obj2 = {a: null};console.log(obj2.a); //null

If we aim to filter out undefined and null, we can use weak comparison with double equality sign(i.e. ==).

console.log(null == undefined); //truelet arr = [null, undefined, 1];let fil = arr.filter(it => {    return it != null;});console.log(fil); //[1]

Reference

Notice

  • If you benefit from this ,Please「Star 」to Support.
  • If you want to follow the latest news/articles for the series of reading notes, Please 「Watch」to Subscribe.

转载地址:http://hipmx.baihongyu.com/

你可能感兴趣的文章
Android性能分析工具简介
查看>>
C#上位机串口控制12864显示
查看>>
制造不同尺寸纳米晶体新方法,或将用于生产柔性彩色显示屏
查看>>
Android Studio工具修理集
查看>>
SpringBoot使用WebJars
查看>>
easyapi
查看>>
Markdown中超链接增加_blank的方法
查看>>
机器人都能造飞机了,你还在呼呼大睡?
查看>>
linux 内核的链表操作(好文不得不转)
查看>>
《泛在服务 ,平台创新》移动电商生态研究报告
查看>>
JVM学习系列:了解JVM options参数配置 & 看懂GC日志
查看>>
12C 对表分区维护的增强
查看>>
算术运算表达式正则及分析
查看>>
linux tcpdump
查看>>
如何实现文件增量同步——算法
查看>>
R.string获取的是数字或者R.integer数字不对的问题
查看>>
dpkg ---- apt-get ------ aptitude 三种方式的区别 及命令格式
查看>>
SQL Server 2008 R2 死锁监控
查看>>
android APP实现更新 PHP后台服务器
查看>>
Oracle 12c 多租户 手工创建 pdb 与 手工删除 pdb
查看>>