Skip to content

Commit e95b04f

Browse files
committed
回家次饭!
1 parent 9d4f399 commit e95b04f

3 files changed

Lines changed: 21 additions & 56 deletions

File tree

JS高级技巧/高级函数.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,9 @@ createXHR运行之后,会被重新改写了,之后的createXHR是改写后
268268

269269
使用的技巧是创建一个匿名、自执行的函数,用以确定应该使用哪一个函数实现。实际的逻辑都一样。不一样的地方就是第一行代码(使用 var 定义函数)、新增了自执行的匿名函数,另外每个分支都返回正确的函数定义,以便立即将其赋值给 createXHR() 。
270270

271-
**惰性载入函数的优点是只在执行分支代码时牺牲一点儿性能。至于哪种方式更合适,就要看你的具体需求而定了。不过这两种方式都能避免执行不必要的代码**
271+
**惰性载入函数的优点是只在执行分支代码时牺牲一点儿性能。至于哪种方式更合适,就要看你的具体需求而定了。不过这两种方式都能避免执行不必要的代码**
272+
273+
# 函数绑定
274+
275+
函数绑定要创建一个函数,可以在特定的 this 环境中以指定参数调用另一个函数。该技巧常常和回调函数与事件处理程序一起使用,以便在将函数作为变量传递的同时保留代码执行环境。
276+

index.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +0,0 @@
1-
//console.log(createXHR.toString());//这么写会报错
2-
var createXHR=(function (){
3-
if(typeof XMLHttpRequest != 'undefined'){
4-
/*return new XMLHttpRequest();*/
5-
return function(){
6-
return new XMLHttpRequest();
7-
}
8-
}else if(typeof ActiveXObject != "undefined"){
9-
return function(){
10-
if (typeof arguments.callee.activeXString != "string"){
11-
var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"];
12-
for (var i=0,len=versions.length; i < len; i++){
13-
try {
14-
new ActiveXObject(versions[i]);
15-
arguments.callee.activeXString = versions[i];
16-
break;
17-
} catch (ex){
18-
//跳过
19-
}
20-
}
21-
}
22-
return new ActiveXObject(arguments.callee.activeXString);
23-
};
24-
}else{
25-
return function(){
26-
throw new Error("浏览器不支持XHR")
27-
};
28-
}
29-
})();
30-
console.log(createXHR.toString());//createXHR已经是改写后的createXHR了
31-
32-
//执行后,createXHR
33-
createXHR();
34-
35-
console.log(createXHR.toString());//createXHR已经是改写后的createXHR了

test-file.html

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,22 @@
55
<title>Document</title>
66
</head>
77
<body>
8-
<p><b>hello </b>word!</p>
8+
9+
<div id="btn">00000000000</div>
10+
11+
12+
13+
914
<script src="index.js"></script>
10-
<!--<script>
11-
function Person(name,age,job){
12-
if(this instanceof Person){
13-
console.log("Person用法 - 正确");
14-
this.name=name;
15-
this.age=age;
16-
this.job=job;
17-
}else{
18-
console.log("Person用法 - 不正确");
19-
return new Person(name,age,job);
15+
<script>
16+
var handler={
17+
message:"Event handler",
18+
handleCLick:function(e){
19+
console.log(this.message);
2020
}
21-
}
22-
var person=Person("hahahah",26,"WEB");
23-
console.log(person);
24-
console.log(person.name);//"hahahah";
25-
console.log(window.name);//""
26-
console.log(typeof window.name);//string
27-
console.log(window.age);//26;
28-
console.log(window.job);//WEB";
29-
</script>-->
21+
};
22+
var oBtn=document.getElementById("btn");
23+
24+
</script>
3025
</body>
3126
</html>

0 commit comments

Comments
 (0)