代码缩进调整

This commit is contained in:
结发受长生
2018-05-10 09:47:30 +08:00
parent 7e0b74a322
commit 368ea5915b
127 changed files with 753 additions and 6168 deletions
@@ -2,10 +2,10 @@
title: 函数形参与arguments
date: 2018-4-14 00:28:25
tags:
- JavaScript
- 函数
- JavaScript
- 函数
categories:
- JavaScript
- JavaScript
---
在之前我们知道 , 在JS当中函数的形参与调用时传入的实参并不需要必须对应
@@ -19,14 +19,14 @@ categories:
调用时对于没有对应到实参的形参进行赋值
```javascript
function func1(arg0, arg1) {
arg1 = 10;
console.log(arg1, arguments[1]);
arg1 = 10;
console.log(arg1, arguments[1]);
}
func1(19);// 10 undefined
function func2(arg0, arg1) {
arguments[1] = 10;
console.log(arg1, arguments[1]);
arguments[1] = 10;
console.log(arg1, arguments[1]);
}
func2(21);// undefined 10
```
@@ -36,14 +36,14 @@ func2(21);// undefined 10
形参与实参有对应的情况
```javascript
function func1(arg0) {
arg0 = 10;
console.log(arg0, arguments[0]);
arg0 = 10;
console.log(arg0, arguments[0]);
}
func1(19);// 10 10
function func2(arg0) {
arguments[0] = 10;
console.log(arg0, arguments[0]);
arguments[0] = 10;
console.log(arg0, arguments[0]);
}
func2(21);// 10 10
```
@@ -54,26 +54,26 @@ func2(21);// 10 10
```javascript
"use strict";
function func1(arg0) {
arg0 = 10;
console.log(arg0, arguments[0]);
arg0 = 10;
console.log(arg0, arguments[0]);
}
func1(19);// 10 19
function func2(arg0) {
arguments[0] = 10;
console.log(arg0, arguments[0]);
arguments[0] = 10;
console.log(arg0, arguments[0]);
}
func2(21);// 21 10
function func3(arg0) {
arguments[0] = 10;
console.log(arg0, arguments[0]);
arguments[0] = 10;
console.log(arg0, arguments[0]);
}
func3();// undefined 10
function func4(arg0) {
arg0 = 10;
console.log(arg0, arguments[0]);
arg0 = 10;
console.log(arg0, arguments[0]);
}
func4();// 10 undefined
```
@@ -2,10 +2,10 @@
title: 变量的定义提升
date: 2018-4-14 00:30:18
tags:
- JavaScript
- 函数
- JavaScript
- 函数
categories:
- JavaScript
- JavaScript
---
+20 -20
View File
@@ -2,10 +2,10 @@
title: 实现bind方法
date: 2018-4-14 00:35:19
tags:
- JavaScript
- prototype
- JavaScript
- prototype
categories:
- JavaScript
- JavaScript
---
@@ -19,11 +19,11 @@ func.bind(thisArg [,arg1[,arg2[, ...]]])
**应用示例 :**
```javascript
var obj = {
name : "Sookie",
show : function() {
console.log(this.name);
console.log(arguments);
}
name : "Sookie",
show : function() {
console.log(this.name);
console.log(arguments);
}
}
var _show = obj.show.bind(obj,"aa","bb");
_show("cc");
@@ -35,16 +35,16 @@ Sookie
如果要实现一个bind方法 , 需要用到柯里化
```javascript
Function.prototype.bind = function(context) {
var me = this;//调用bind的函数对象
var args = Array.prototype.slice.call(arguments, 1);//传入的固定实参
return function () {
//实际调用时传入的参数
var innerArgs = Array.prototype.slice.call(arguments);
//合并两个参数数组
var finalArgs = args.concat(innerArgs);
//调用该函数
return me.apply(context, finalArgs);
}
var me = this;//调用bind的函数对象
var args = Array.prototype.slice.call(arguments, 1);//传入的固定实参
return function () {
//实际调用时传入的参数
var innerArgs = Array.prototype.slice.call(arguments);
//合并两个参数数组
var finalArgs = args.concat(innerArgs);
//调用该函数
return me.apply(context, finalArgs);
}
}
```
这里解释一下 , arguments并不是数组对象 , 而是**类数组**
@@ -64,7 +64,7 @@ var args = Array.prototype.slice.call(arguments);
( 可能是出于兼容性的需要 , 比如旧版本的标准库当中无此函数 )
```javascript
Function.prototype.bind = Function.prototype.bind || function(context) {
//...
//...
}
```
这属于一个加分项
@@ -82,6 +82,6 @@ Function.prototype.bind = Function.prototype.bind || function(context) {
由于需要调用bind方法的一定是一个函数 , 所以有必要在bind的内部做一个校验
```javascript
if(typeof this !== "function") {
throw new TypeError("what is trying to be bound is not callable");
throw new TypeError("what is trying to be bound is not callable");
}
```
+35 -35
View File
@@ -2,10 +2,10 @@
title: 实现call方法
date: 2018-4-14 00:56:44
tags:
- JavaScript
- prototype
- JavaScript
- prototype
categories:
- JavaScript
- JavaScript
---
> call方法在使用一个指定的this值和若干个指定参数值的前提下调用某个函数
@@ -13,10 +13,10 @@ categories:
常规调用方式
```javascript
var obj = {
name : "Sookie",
func : function(msg) {
console.log(this.name + " : " + msg);
}
name : "Sookie",
func : function(msg) {
console.log(this.name + " : " + msg);
}
}
var fn1 = obj.func;
fn1.call(obj, "Hello");
@@ -27,9 +27,9 @@ fn1.call(obj, "Hello");
#### 雏形
```javascript
Function.prototype.call2 = function(context) {
context.fn = this;
context.fn();
delete context.fn;
context.fn = this;
context.fn();
delete context.fn;
}
```
初步实现了不传参情况下的函数调用
@@ -42,13 +42,13 @@ Function.prototype.call2 = function(context) {
```javascript
Function.prototype.call2 = function(context) {
context.fn = this;
var args = [];
for(let i=1 ; i<arguments.length ; i++) {
args.push("arguments[" + i + "]");
}
eval("context.fn(" + args + ")");
delete context.fn;
context.fn = this;
var args = [];
for(let i=1 ; i<arguments.length ; i++) {
args.push("arguments[" + i + "]");
}
eval("context.fn(" + args + ")");
delete context.fn;
};
```
比如实际调用函数的时候需要传入2个参数 , 包括前面的context , 所以arguments当中就有3个元素
@@ -64,23 +64,23 @@ context.fn(arguments[1], arguments[2])
可以预先把这个对象保存下来
```javascript
Function.prototype.call2 = function(context) {
var _fn = null;
var flag = false;
if("fn" in context) {
_fn = context.fn;
}
_fn = context.fn;
context.fn = this;
var args = [];
for(let i=1 ; i<arguments.length ; i++) {
args.push("arguments[" + i + "]");
}
var result = eval("context.fn(" + args + ")");
if(flag) {
context.fn = _fn;
} else {
delete context.fn;
}
return result;
var _fn = null;
var flag = false;
if("fn" in context) {
_fn = context.fn;
}
_fn = context.fn;
context.fn = this;
var args = [];
for(let i=1 ; i<arguments.length ; i++) {
args.push("arguments[" + i + "]");
}
var result = eval("context.fn(" + args + ")");
if(flag) {
context.fn = _fn;
} else {
delete context.fn;
}
return result;
};
```