-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,36 @@ | ||
// @ts-nocheck | ||
var proxy = new Proxy({}, { | ||
get(target, propKey){ | ||
return 35 | ||
} | ||
}) | ||
// var proxy = new Proxy({}, { | ||
// get(target, propKey){ | ||
// return 35 | ||
// } | ||
// }) | ||
|
||
// var obj = { | ||
// a: 1, | ||
// b: 2 | ||
// } | ||
|
||
// const data = new Proxy(obj, { | ||
// get(target, key, weizhi){ | ||
// console.log(target, key, weizhi) | ||
// return target[key] | ||
// }, | ||
// set(target,) | ||
// }) | ||
|
||
var obj = new Proxy({}, { | ||
get(target, key, obj) { | ||
console.log(target) | ||
console.log(key) | ||
console.log(obj) | ||
// return target[key] | ||
return Reflect.get(target, key) | ||
}, | ||
set(target, key, val, obj) { | ||
console.log(target) | ||
console.log(key) | ||
console.log(val) | ||
console.log(obj) | ||
target[key] = val | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Function.prototype.myApply = function(context, arr) { | ||
if(typeof this !== 'function') { | ||
return 'err' | ||
} | ||
context = context || window | ||
context.fn = this | ||
let res | ||
if(!arr) { | ||
res = context.fn() | ||
} | ||
res = context.fn(...arr) //如果有返回值 | ||
delete context.fn | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//简易版本 | ||
// 返回一个函数 可以传入参数 | ||
Function.prototype.myBind = function(context) { | ||
var _this = this | ||
var context = context || window | ||
//取出bind的第二个到后面的参数 | ||
var args = [...arguments].slice(1) | ||
return function () { | ||
return _this.apply(context, args.concat(...arguments)) | ||
} | ||
} | ||
//如果有构造函数 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Function.prototype.mycall = function(context) { | ||
if(typeof this !== 'function') { | ||
return 'err' | ||
} | ||
context = context || window | ||
context.fn = this | ||
const arrs = [...arguments].slice(1) | ||
const res = context.fn(...arrs) //如果有返回值 | ||
delete context.fn | ||
return res | ||
} | ||
|
||
function ff(){ | ||
console.log(this.name) | ||
return 1 | ||
} | ||
const obj = { | ||
name: 'mycall' | ||
} | ||
|
||
ff.mycall(obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function copyNew(){ | ||
const obj = {} | ||
const Constructor = [...arguments][0] | ||
obj.__proto__ = Constructor.prototype | ||
const res = Constructor.apply(obj,[...arguments].slice(1)) | ||
return typeof res === 'object' ? res : obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>从原型到原型链</title> | ||
</head> | ||
<body> | ||
<script> | ||
|
||
function Person() { | ||
|
||
} | ||
Person.prototype.name = 'cc' | ||
var per1 = new Person() | ||
var per2 = new Person() | ||
console.log(per1.name) | ||
console.log(per2.name) | ||
//每个函数都有一个prototype属性,这个属性指向了一个对象,这个对象正是调用该构造函数而创建到实例的原型,也就是这个例子当中per1,per2的原型。 | ||
//每一个JavaScript对象(null除外)在创建的时候就会与之关联另一个对象,这个对象就是我们所说的原型,每一个对象都会从原型"继承"属性,这个对象都具有的一个属性,叫做__proto__。 | ||
console.log(per1.__proto__ === Person.prototype) | ||
console.log(per2.__proto__ === Person.prototype) | ||
//原型下的constructor 指向当前构造函数 | ||
console.log(Person.prototype.constructor === Person) | ||
console.log(Object.getPrototypeOf(per1) === Person.prototype) | ||
|
||
console.log(Object.prototype.__proto__ === null) | ||
|
||
//原型链 | ||
per1.__proto__ => Person.prototype,然后 Person.prototype.__proto__ => Object.prototype ,最后 Object.prototype.__proto__ => null | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |