一个利用闭包实现的函数参数重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function addMethod(object, name, fn) {
var old = object[name];
object[name] = function () {
console.log(fn.length, "&", arguments.length);
if (fn.length == arguments.length) {
console.log("fn", fn);
return fn.apply(this, arguments);
} else if (typeof old == "function") {
console.log("old", old);
return old.apply(this, arguments);
} else {
console.log("error");
}
};
}
var a = {};
addMethod(a, "eat", function (food) {
console.log("eat2: ", food);
return arguments;
});
addMethod(a, "eat", function () {
console.log("eat吃");
return arguments;
});
addMethod(a, "eat", function (food, num) {
console.log("eat3: ", food, " ", num, "次");
return arguments;
});
// console.log("a.eat", a.eat);
console.log(a.eat("apple", 2));
console.log("=============================");
console.log(a.eat());
console.log("=============================");
console.log(a.eat("apple"));
console.log("=============================");

blob转dataUrl大于2M导致下载失败。使用

1
2
3
4
/*这个 URL 的生命周期和创建它的窗口中的 document 绑定。*/
objectURL = URL.createObjectURL(object);
/*结束使用某个 URL 对象之后,应该通过调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。*/
URL.revokeObjectURL(objectURL);

响应头 Content-Disposition

1
2
3
Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"