Skip to content

Commit

Permalink
0318 learn
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Mar 18, 2020
1 parent 9192188 commit b30f151
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 5 deletions.
Binary file modified .DS_Store
Binary file not shown.
39 changes: 34 additions & 5 deletions ES6/proxy.js
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
}
})
14 changes: 14 additions & 0 deletions 手写一个apply.js
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
}
13 changes: 13 additions & 0 deletions 手写一个bind.js
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))
}
}
//如果有构造函数

21 changes: 21 additions & 0 deletions 手写一个call.js
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)
7 changes: 7 additions & 0 deletions 手写实现new.js
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
}
15 changes: 15 additions & 0 deletions 深入系列/作用域.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ <h1>相对的动态作用域,函数的作用域在函数调用的时候才决
前面我们已经说了,JavaScript采用的是静态作用域,所以这个例子的结果是 1。
*/

/*
testVo:{
arg:{
0:1,
len: 1
},
a: 10,
b: unde,
c: unde, => 10
d: func,
e: unde, => func
}
*/
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions 深入系列/原型到原型链.html
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>
11 changes: 11 additions & 0 deletions 深入系列/深入之浮点数精度.html
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>

0 comments on commit b30f151

Please sign in to comment.