forked from daige/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter2.js
More file actions
265 lines (186 loc) · 3.63 KB
/
Chapter2.js
File metadata and controls
265 lines (186 loc) · 3.63 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//语法
/*
var Daige = "hello";
var daige = "hello";
alert(daige);
alert(Daige);
alert(typeof(daige));
daige = 1234;
alert(typeof(daige));
//变量
var daige = "hi",age=25;
alert(typeof daige);
alert(typeof age);
test=daige;
alert(test);
//原始值和引用值
alert(typeof "hi daige");
alert(typeof null); //返回object
string = 1+"20";
alert(typeof string);
alert(typeof String); //返回function
var temp;
alert(temp);
alert(typeof temp);
alert(typeof temp1); //temp1未定义,其他运算符出错
function daige()
{
}
alert(typeof daige()); //alert(typeof daige)返回function
alert( null == undefined);
var temp;
alert( null == typeof(temp)); //false
alert( null == typeof(temp1));
alert(true);
alert(1==1);
alert(true < false);
alert(true > 0);
var t = 0x1023;
alert(t);
var f = 3.14e10;
alert(f);
alert(typeof f);
alert(isFinite(f*20000));
alert(isNaN("daige"));
var str1 = "你 好";
var str2 = "daig\"e";
var str3 = 'd';
alert(str1);
alert(str2);
alert(str3);
//转换
var num = 10;
alert(num.toString());
var isTrue = true;
alert(isTrue.toString());
var strTest = "daige";
alert(strTest.toString());
alert(num.toString(16));
alert(parseInt("123daige"));
alert(parseInt("13",16));
alert(parseInt("22.5"));
alert(parseInt("daige"));
alert(parseFloat("12.345"));
alert(Boolean("daige"));
alert(Number("23.21232"));
alert(String(123));
alert(Number("23.4.5"));
alert(parseInt("23.4.5"));
alert(String(null));
alert(String(undefined));
//引用类型
var obj = new String("daige");
alert(obj.length);
alert(obj.charAt(1));
alert(obj.concat(" hello"))
alert(obj.localeCompare());
var str1=new String("hi daige");
alert(str1.substring(2));
alert(str1.toLocaleUpperCase());
var test = new String("helo world");
alert(test instanceof String);
//运算符
var test = new String("daige");
test.name = "D";
alert(test.name);
delete test.name;
alert(test.name);
var str = "25";
str = -str;
alert(str);
alert(typeof str);
var num = 18;
alert(num.toString(2)); //10010
num = ~num; //1*27 01101
alert(num.toString());
var num = 25 & 3;
alert(num);
var num = 25 | 3;
alert(num);
var num = 25 ^ 3;
alert(num);
var num = -2;
alert(num << 5);
var comp = "Block" < "about";
alert(comp);
var comp = "Block".toLowerCase() < "about".toLowerCase();
alert(comp);
var num = "23" < "3";
alert(num);
var num = "23" < 3;
alert(num);
var str = "55";
var num = 55;
alert(str == num);
alert(str === num);
start: var num = 0;
var str = "bule";
switch(str)
{
case "blue":
break start;
case "daige":
var num = 100;
break;
default:
var num = 50;
}
alert(num);
MYLOOP: do {
x = x + 1;
if (x > 100) break MYLOOP;
continue MYLOOP;
} while (0)
alert(x);
//函数
function test(num1,num2)
{
return num1+ num2;
}
function test1(num1)
{
num3 = num1;
}
alert(test(1,2));
alert(test1(1));
function test()
{
alert(arguments[0]);
alert(arguments.length);
}
alert(test("daige","hi"));
function add()
{
if(arguments.length == 2)
{
return arguments[0]+arguments[1];
}
else if (arguments.length == 3)
{
return arguments[0]+arguments[1]+arguments[2];
}
else
return false;
}
alert(add(1,2));
alert(add(1,2,3));
var doAdd = new Function("num1","num2","alert( num1+ num2)");
function test(addFunction,num1,num2)
{
addFunction(num1,num2);
}
alert(test(doAdd,1,2));
alert(doAdd.length);
alert(doAdd.valueOf());
alert(doAdd.toString());
var totalNum = 10;
function addNum(num1,num2)
{
function doAdd() //doAdd是一个闭包 它获取外部函数参数和全局变量
{
return num1+num2+totalNum;
}
return doAdd(); //addNum调用内部函数 闭包不接受参数,它使用的的值是从执行环境获取
}
alert(addNum(1,2));
*/