大涛子

Results 99 issues of 大涛子

## 创建对象的多种方式以及优缺点 这篇文章更像是笔记,因为《JavaScript 高级程序设计》写得真是太好了! ### 工厂模式 ```js function createPerson(name) { var o = new Object(); o.name = name; o.getName = function() { console.log(this.name); }; return o; } var person1 =...

JavaScript 深入系列

## call 和 apply 的模拟实现 ### call `call()`在使用一个指定的 this 值和若干个指定的参数值的前提下,调用某个函数或方法。该方法的语法和作用与 apply() 方法类似,只有一个区别,就是 call() 方法接受的是一个参数列表,而 apply() 方法接受的是一个包含多个参数的数组。 使用 call 方法调用函数并且指定上下文的 'this' ```js var value = 1; var obj = { value:...

JavaScript 深入系列

## 执行上下文 对于每个执行上下文,都有三个重要属性: - 变量对象(Variable object,VO) - 作用域链(Scope chain) - this ### 分析第一段代码 ```js var scope = "global scope"; function checkscope() { var scope = "local scope"; function f() {...

JavaScript 深入系列

## 前言 主要有:new、Object.create()、Object.setPrototypeOf()、instanceof、class、type API、原型链继承等。 ## new 实现 我们看下 new 做了什么: 1. 创建一个新对象; 1. 将构造函数的作用域赋给新对象(因此 this 就指向了这个新对象) 1. 执行构造函数中的代码(为这个新对象添加属性) 1. 返回新对象。 ```js function objectFactory() { // 1. 新建一个对象 obj const obj...

手写系列

## bind 实现 我们可以首先得出 bind 函数的两个特点: - 返回一个函数 - 可以传入参数 如: ```js var foo = { value: 1 }; function bar(name, age) { console.log(this.value); console.log(name); console.log(age); } var bindFoo =...

手写系列

## 继承的多种方式和优缺点 ### 原型链继承 ```js function Parent() { this.name = 'Yang'; } Parent.prototype.getName = function() { return this.name; } function Child() { } Child.prototype = new Parent(); var myself =...

JavaScript 深入系列

## 原型链继承实现 ### 用法 ```js function Parent(name) { this.name = name; } Parent.prototype.getName = function() { return this.name; }; function Child(name, age) { Parent.call(this, name); this.age = age; } Child.prototype...

JavaScript 深入系列

## 类数组对象与 arguments 类数组对象从读写、获取长度、遍历三个方面看,和数组貌似是一样的,但是无法直接使用数组的方法,需要借助 call 或 apply: ```js var likeArr = { 0: "a", 1: "b", 2: "c", length: 3 }; Array.prototype.slice.call(likeArr, 0); // ["a", "b", "c"] Array.prototype.join.call(likeArr, "&"); //...

JavaScript 深入系列

## 前言 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象类型之一。 我们看下 new 做了什么: 1. 创建一个新对象; 1. 将构造函数的作用域赋给新对象(因此 this 就指向了这个新对象) 1. 执行构造函数中的代码(为这个新对象添加属性) 1. 返回新对象。 ```js function Person(name, age) { this.name = name; this.age = age; this.habit =...

JavaScript 深入系列

# JavaScript深入之bind的模拟实现 ## 基本实现 我们可以首先得出 bind 函数的两个特点: - 返回一个函数 - 可以传入参数 如: ```js var foo = { value: 1 }; function bar(name, age) { console.log(this.value); console.log(name); console.log(age); } var bindFoo...

JavaScript 深入系列