Browse Source

first commit

DESKTOP-MK04A0R\chuck 3 years ago
commit
ddc11cf695
9 changed files with 146 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 24 0
      01/index.html
  3. 14 0
      01/src/call.js
  4. 24 0
      02/index.html
  5. 14 0
      02/src/apply.js
  6. 28 0
      03/index.html
  7. 21 0
      03/src/bind.js
  8. 11 0
      README.md
  9. 8 0
      package.json

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+
+node_modules/

+ 24 - 0
01/index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="./src/call.js"></script>
+    <script>
+        function add (a,b){
+            console.log(this)
+            return a + b + this.c
+        }
+        let obj = {
+            c: 5
+        }
+        window.c = 6
+        console.log(call(add,obj,1,2))
+        console.log(call(add,null,1,2))
+    </script>
+</body>
+</html>

+ 14 - 0
01/src/call.js

@@ -0,0 +1,14 @@
+function call(fn,obj,...args){
+    //判断
+    if(obj === null || obj === undefined){
+        obj = globalThis //全局对象 es11里的
+    }
+    //给obj赋予临时函数
+    obj.temp = fn;
+    //调用函数
+    let result = obj.temp(...args)
+    //销毁temp
+    delete obj.temp
+    //返回结果
+    return result
+}

+ 24 - 0
02/index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="./src/apply.js"></script>
+    <script>
+        function add (a,b){
+            console.log(this)
+            return a + b + this.c
+        }
+        let obj = {
+            c: 5
+        }
+        window.c = 6
+        console.log(apply(add,obj,[1,2]))
+        console.log(apply(add,null,[1,2]))
+    </script>
+</body>
+</html>

+ 14 - 0
02/src/apply.js

@@ -0,0 +1,14 @@
+function apply(fn,obj,args){
+    //判断
+    if(obj === undefined || obj === null){
+        obj = globalThis
+    }
+    //为obj添加临时方法
+    obj.temp = fn
+    //执行函数
+    let result = obj.temp(...args)
+    //删除临时函数
+    delete obj.temp
+    //返回结果
+    return result
+}

+ 28 - 0
03/index.html

@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="./src/bind.js"></script>
+    <script>
+        function add (a,b){
+            console.log(this)
+            return a + b + this.c
+        }
+        let obj = {
+            c: 5
+        }
+        window.c = 6
+        //两种
+        let fn = bind(add,obj,1,2)
+        console.log(fn())
+
+        let fn2 = bind(add,obj)
+        console.log(fn2(1,2))
+    </script>
+</body>
+</html>

+ 21 - 0
03/src/bind.js

@@ -0,0 +1,21 @@
+function call(fn,obj,...args){
+    //判断
+    if(obj === null || obj === undefined){
+        obj = globalThis //全局对象 es11里的
+    }
+    //给obj赋予临时函数
+    obj.temp = fn;
+    //调用函数
+    let result = obj.temp(...args)
+    //销毁temp
+    delete obj.temp
+    //返回结果
+    return result
+}
+function bind(fn,obj,...args){
+    //返回新函数
+    return function(...args1){
+        return call(fn,obj,...args,...args1)
+    }
+}
+

+ 11 - 0
README.md

@@ -0,0 +1,11 @@
+## 目录说明
+* 01 call函数封装实现
+* 02 apply函数封装实现
+* 03 bind函数封装实现
+
+## 运行
+npm i
+npm start
+
+## 关于
+自定义函数工具库

+ 8 - 0
package.json

@@ -0,0 +1,8 @@
+{
+  "scripts": {
+    "start": "http-server -a 127.0.0.1 -p 7070"
+  },
+  "devDependencies": {
+    "http-server": "^13.0.1"
+  }
+}