/
Function.html
118 lines (86 loc) · 3.03 KB
/
Function.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数</title>
<meta charset="utf-8" />
</head>
<body>
<script>
"use strict";
/**
* Simple
* arguments: 参数集
*/
function call(log) {
var __name__ = "call";
//this.name = "call function";
this.desc = "test for function.";
console.log(arguments);
console.log(log);
return this.desc;
}
//call('123', { name: 'Lucker', age: 123 });
//Arguments[2] / ["123", Object]
//"123"
//console.log(call.toString());
//function call(log) {
// console.log(arguments)
// console.log(log);
//}
//console.log(call.__name__); //undefined
//console.log(call.name); //"call"
//console.log(call.desc); //undefined
//var callA = call('123');
//console.log(callA.desc); //Function.html:40 Uncaught TypeError: Cannot read property 'desc' of undefined
//var callB = call();
//console.log(callB); //"test for function."
function isFunction(func) {
return typeof func === 'function';
}
//console.log(isFunction(call)); //true
/**
* prototype 属性使您有能力向对象添加属性和方法。
* https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
*/
function MyObject(name, message) {
this.name = String(name);
this.message = String(message);
}
MyObject.prototype.getName = function () {
return this.name;
}
MyObject.prototype.getMessage = function () {
return this.message;
}
var myObj =new MyObject('JavaScript Object', 'do something');
//console.log(myObj.getName()); //"JavaScript Object"
//console.log(myObj.getMessage()); // "do something"
//console.log(new MyObject("abc", "Lor").getName()); // "abc"
//console.log(MyObject.prototype); //"Object { constructor: function(name, message), getMessage: function(), getName: function(), __proto__: {} }"
//console.log(typeof myObj); // "object"
//console.log(myObj instanceof MyObject); // true
function Circle(r) {
this.r = r;
}
Circle.PI = 3.14159;
Circle.prototype.area = function () {
return Circle.PI * this.r * this.r;
}
var c = new Circle(2.0);
//console.log(c.area()); // "12.56636"
//全局属性 Infinity 是一个数值,表示无穷大。
//console.log(Infinity); "Infinity"
//回调函数 callback
//函数表达式
var printStr = function(val){
console.log(val);
}
//函数声明
// function printStr(val){
// console.log(val);
// }
printStr('123');
</script>
</body>
</html>