记录一些琐碎的知识点。
基本数据类型
- Boolean
- Number
- String
- null
- undefined
- Symbol
声明
- 使用关键字var:函数作用域
- 使用关键字let:块作用域
- 直接使用:全局作用域
只声明不赋值,变量默认值是undefined
const关键字可以声明不可变变量,同样为块作用域。对不可变的理解在对象上的理解需要注意。12345678const num =1;const obj = {prop: 'valuye'}num = 2; // Uncaught TypeError: Assignment to constant variable.obj['prop'] = 'value2';obj = []; // Uncaught TypeError: Assignment to constant variable.
变量提升
Javascript中可以引用稍后声明的变量,而不会引发异常。
函数
定义函数
- 函数声明
- 函数表达式
- Function 构造函数
- 箭头函数1234function fn () {}var fn = function () {}var fn = new Function(arg1, arg2, ... argN, funcBody)var fn = (param) => {}
arguments
arguments: 一个包含了传递给当前执行函数参数的类似于数组的对象
arguments.length: 传递函数的参数的数目
rest
|
|
默认值
|
|
对象
javacript 中对象是可变键控集合
定义对象
- 字面量
- 构造函数12345var obj = {prop: 'value'fn: function () {}};var date = new Date();
构造函数
构造函数和普通函数并没有区别,使用new关键字调用就是构造函数,使用构造函数可以实例化一个对象
函数的返回值有两种可能
- 显示调用 return 返回 return 后表达式的求值
- 没有调用 return 返回 undefined
构造函数返回值
- 没有返回值
- 简单数据类型
- 对象类型
前两种情况构造函数返回构造对象的实例,实例化对象正式利用的这个特性
第三种构造函数和普通函数一致,返回 return 后表达式的结果prototype
每个函数都有一个 prototype 的对象属性,对象内又一个 constructor 属性,默认指向函数本身
每个对象都有一个 proto 的属性,属相指向其父类的 prototype12345678function Person (name) {this.name = name;}Person.prototype.print = function () {console.log(this.name);}var p1 = new Person('DogJun');p1.print();
this和作用域
作用域可以通俗的理解
- 我是谁
- 我有哪些马仔
其中我是谁的回答就是 this
马仔就是我的局部变量
this 场景
Function.prototype.bind
bind 返回一个新函数,函数的作用域为 bind 参数
()=>{}
箭头函数是ES6提供的新特性,是简写的函数表达式,拥有词法作用域和 this 值
继承
在javascript的场景,继承有两个目标,子类需要得到父类的:
- 对象的属性
- 对象的方法1234567891011121314151617181920212223function inherits (child, parent) {var _prototype = Object.create(parent.prototype);_prototype.constructor = child.prototype.constructor;child.prototype= _prototype;}function People (name, age) {this.name = name;this.age = age;}People.prototype.getName = function () {return this.name;}function English (name, age, language) {People.call(this, name, age);this.language = language;}inherits(English, People);English.prototype.introduce = function () {console.log('Hi, I am ' + this.getName());console.log('I speak ' + this.language);}var en = new English('DogJun', 24, 'English');en.introduce();
ES6 class 与继承
|
|
高阶函数
高阶函数是把函数当做参数或者返回值是函数的函数
回调函数
|
|
闭包
闭包由两部分组成
- 函数
- 环境:函数创建时作用域内的局部变量
|
|
典型错误
正确实现
惰性函数
|
|
柯里化
一种允许使用部分参数生成函数的方式
反柯里化
|
|
push 通用化