-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht1.js
699 lines (558 loc) · 13.1 KB
/
t1.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/**
* Created by CAIWL on 2016/8/25.
*/
var say = function () {
var num = 0;
var s = function () {
num++;
return num;
}
return s;
}
//var says = say();
//console.log(says());
function abc(key) {
/*switch (true){
case key>10&&key<=20:
console.log(1)
break;
case key>20:
console.log(2)
break;
}*/
/* if(key>10&& key <20){
console.log(1)
}else if(key >20) {
console.log(2)
}else{
console.log(3)
}*/
var a = 'aaa', b = a;
b = '3333';
var obj = new Object();
var obj2 = obj;
obj2.name = 'name';
console.log(obj.name)
console.log(a)
console.log(b)
}
//abc(11);
/*(function () {
var a = b = 5;
})();
console.log('b:', b);*/
/*var fullName = 'John Doe';
var obj = {
fullName: 'Colin Ihrig',
prop: {
fullName: 'Aurelio De Rosa',
getFullName: function () {
return this.fullName;
}
}
};
console.log('========================');
console.log(obj.prop.getFullName());
var test = obj.prop.getFullName;
console.log(test())
console.log(test.call(obj));*/
/*
function setName(obj){
obj.name = 'winnie';
obj = new Object();
obj.name = 'ddfdf';
}
var person = new Object();
setName(person);
console.log(person.name);*/
/*function buildUrl(){
var gs = "?debug=true";
with(location){
var url = href +gs;
}
return url;
}
console.log(buildUrl())*/
/*
var a = "";
console.log(a.constructor == String)*/
/*
function itemTest() {
var item = new Array("one", "two","three");
var itemRef = item;
item.push("four")
console.log(item.length === itemRef.length)
var a = "jjjjjjjjjjj";
var aRef = a;
a+="ddddd";
console.log(a)
console.log(aRef)
}
itemTest();*/
/*
var f = function() {
return 23;
}
var foo = "abc";
if(true){
var foo = "test";
}
alert(foo);
function ftest() {
var foo = "old ddd"
}
ftest()
alert(foo == "test")*/
/*
function add(num){
return function (toAdd) {
return num+toAdd
}
}
var test = add(5)(4);
alert(test)*/
//console.log(test);
/*
function Person(students, teacher){
var person = this;
function disp(){
alert(this.names = students.join('-'))
}
this.students = students;
this.teacher = teacher;
disp()
}
var a = new Person(['cai','winnie'],['weili']);
console.log(a.names);*/
/*function User(name, age) {
var year = new Date().getFullYear() - age;
this.getYearBorn = function () {
return year;
}
}
var user = new User('winnie', 26);
alert(user.getYearBorn())*/
/*function User(properties) {
console.dir(properties)
for(var i in properties){
(function (which) {
var p= i
which['set'+ p] = function (val) {
properties[p] = val;
}
which['get'+p] = function () {
return properties[p]
}
})(this)
}
}
var params = {
name:"winnie",
age:"27"
}
var user = new User(params)
user.setage(222)
console.log(user.getage());
console.log(params.age);
params.name="caiwinnie"
console.log(user.getname());*/
/*
function f(num) {
if (num <= 1)
return 1;
else
return num * f(num - 1);
}
console.log(f(5));
function f2(num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num - 1)
}
}
console.log(f2(5));*/
/*
function outer() {
inner();
}
function inner() {
console.log(inner.caller);
}
outer();*/
/*
function htmlEscape(text) {
return text.replace(/[<>"&]/g, function (match, pos, originalText) {
switch (match){
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """;
}
});
}
console.log(htmlEscape("<p class=\"greeting\">&Hello world!</p>"));*/
/*
var stringValue = "yellow";
console.log(stringValue.localeCompare('brick'));
console.log(stringValue.localeCompare('yellow'));
console.log(stringValue.localeCompare('zoo'));
*/
//console.log(String.fromCharCode(104, 101, 108, 108, 111));
/*
var uri = "http://www.baidu.com/idea value.html#start";
console.log(encodeURI(uri));
console.log(encodeURIComponent(uri));*/
/*eval("console.log('hi')");*/
/*
var max = Math.max.apply(Math, [3, 52, 23, 64])
console.log(max);
console.log(Math.floor(Math.random() * 100 + 1));*/
/*
var person ={};
Object.defineProperty(person, "name", {
configurable: false,
value:"winnie"
});
console.log(person.name);
person.name= "caiweili";
console.log(person.name);*/
/*var book = {
_year: 2004,
edition: 1
}
Object.defineProperty(book, 'year', {
get: function () {
return this._year;
},
set: function (newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
});
console.log(book);
book.year = 2010;
console.log(book);*/
/*var person = {
_year: 2004,
year: 0,
edition:1
};
Object.defineProperties(person, {
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function () {
return this._year;
},
set: function (newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004
}
}
}
});
console.log(person);//{ _year: 2004, year: [Getter/Setter], edition: 1 }
person.year = 2010;
console.log(person);//{ _year: 2010, year: [Getter/Setter], edition: 7 }
var descript = Object.getOwnPropertyDescriptor(person, '_year');
var descript1 = Object.getOwnPropertyDescriptor(person, 'year');
console.log(descript.value);//2010
console.log(descript.configurable); //true
console.log(descript.enumerable);//true
console.log(descript1.value);//undefined
console.log(descript1.configurable);//true
console.log(descript1.enumerable);//true
console.log(typeof descript1.get);//function*/
/*function Person(){}
Person.prototype.name = "Nicholas";
var person1 = new Person();
var person2 = new Person();
// console.log(person1.hasOwnProperty('name')); //false
// console.log('name' in person1); // true
person1.name = "winnie";
// console.log(person1.name); // winnie
// console.log(person1.hasOwnProperty('name')); //true
// console.log('name' in person1); // true
// console.log(person2.name); // Nicholas
// console.log(person2.hasOwnProperty('name')); //false
// console.log('name' in person2); // true
delete person1.name;
console.log(person1.name); // Nicholas
console.log(person1.hasOwnProperty('name')); //false
console.log('name' in person1); // true
*/
/* function Person() {
}
Person.prototype.name = 'winnie';
Person.prototype.age = 26;
Person.prototype.job = 'it';*/
/*
var keys = Object.keys(Person.prototype);
console.log(keys);[ 'name', 'age', 'job' ]
var p1 = new Person();
p1.name="caiwili";
p1.age = 20;
var pkeys = Object.keys(p1);
console.log(pkeys); // ['name', 'age']
*/
// var keys = Object.getOwnPropertyNames(Person.prototype);
// console.log(keys); //[ 'constructor', 'name', 'age', 'job' ]
/*
function Person(){};
Person.prototype ={
constructor: Person,
name:"winnie",
age: 18,
job:"IT",
sayName: function () {
console.log(this.name);
}
}
var friend = new Person();
console.log(friend instanceof Object); //true
console.log(friend instanceof Person); //true
console.log(friend.constructor == Person); //false
console.log(friend.constructor == Object); //true
*/
/*function Person(){};
Person.prototype ={
constructor: Person,
name:"winnie",
age: 18,
job:"IT",
friends:['ssss','ddddd'],
sayName: function () {
console.log(this.name);
}
};
var person1 = new Person();
var person2 = new Person();
person1.friends.push('dfddfd');
console.log(person1.friends);
console.log(person2.friends);
console.log(person1.friends === person2.friends);
*/
/*
function Person(name, age, job) { //构造函数
this.name = name;
this.age = age;
this.job = job;
this.friends= ['Vibo', 'H.Cong']
}
Person.prototype = { //原型构造
constructor: Person,
sayName: function () {
console.log(this.name);
}
}
var person1 = new Person('winnie', 18, 'IT');
var person2 = new Person('winnie2', 182, 'IT2');
person1.friends.push('Van');
console.log(person1.friends); //[ 'Vibo', 'H.Cong', 'Van' ]
console.log(person2.friends); //[ 'Vibo', 'H.Cong' ]
console.log(person1.friends === person2.friends);//false
console.log(person1.sayName === person2.sayName);//true
*/
/*
function Person(name, age, job){ //寄生模式
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function () {
console.log(this.name);
};
return o;
}
*/
/*
function Person(name) { //稳妥构造函数模式
var o = new Object();
o.sayName = function () {
console.log(name);
}
return o;
}
*/
/*function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function () {
return this.property;
}
function SubType() {
this.subproperty = false;
}
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function () {
return this.subproperty;
}
var instance = new SubType();
console.log(instance.getSuperValue()); // true
console.log("==========================");
console.log(instance instanceof Object); //true
console.log(instance instanceof SuperType); //true
console.log(instance instanceof SubType); //true
console.log("==========================");
console.log(Object.prototype.isPrototypeOf(instance)); //true
console.log(SuperType.prototype.isPrototypeOf(instance)); //true
console.log(SubType.prototype.isPrototypeOf(instance)); //true
console.log("=========================");
SubType.prototype.getSuperValue = function () {
return false;
}
console.log(instance.getSuperValue()); // false*/
/*
function SuperType() {
this.colors = ['red', 'blue', 'green']
}
function SubType(){
// 借用构造函数
//SuperType.call(this); //有无这段话影响很大
//SuperType.apply(this); //有无这段话影响很大
}
SubType.prototype = new SuperType();
var instance1 = new SubType(); //够着函数还是指向了SuperType
instance1.colors.push('black');
console.log(instance1.colors);//[ 'red', 'blue', 'green', 'black' ]
var instance2 = new SubType();
console.log(instance2.colors);//[ 'red', 'blue', 'green', 'black' ]
*/
//组合继承
/*function SuperType(name) {
this.name = name;
this.colors= ['red','blue','green'];
}
SuperType.prototype.sayName =function () {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function () {
console.log(this.age);
}
var instance1 = new SubType('winnie', 27);
instance1.colors.push('black');
console.log(instance1.colors);//[ 'red', 'blue', 'green', 'black' ]
instance1.sayName();//winnie
instance1.sayAge();//27
var instance2 = new SubType('dfdf', 3333);
console.log(instance2.colors);//[ 'red', 'blue', 'green' ]
instance2.sayName();//dfdf
instance2.sayAge();//3333*/
//原型式继承
/*
function object(o) {
function F() {
}
F.prototype = o;
return new F();
}
var person = {
name: "winnie",
friends: ['shelby', 'court', 'van']
}
var anotherPerson = object(person);
anotherPerson.name = "greg";
anotherPerson.friends.push('rob');
console.log(anotherPerson);
var yetAnotherPerson = object(person);
yetAnotherPerson.name = 'Linda';
yetAnotherPerson.friends.push('dfd');
console.log(yetAnotherPerson);
console.log(person);*/
//构造函数模式
/*
function SuperType(name) {
this.name = name;
}
function SubType() {
SuperType.call(this, "Nicholas"); //SubType的复用性就没有了
this.age = 29;
}
var instance = new SubType();
console.log(instance.name);
console.log(instance.age);
*/
//组合继承
/*function SuperType(name) {
this.name = name;
this.colors= ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
};
function SubType(name, age) {
//继承属性
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = SuperType.prototype;
SubType.prototype.sayAge = function () {
console.log(this.age);
};
var instance1 = new SubType("Winnie", 29);
instance1.colors.push('black');
console.log(instance1.colors);
instance1.sayName();
instance1.sayAge();
var instance2 = new SubType('Winnie2', 27);
console.log(instance2.colors);
instance2.sayName();
instance2.sayAge();*/
//原型式继承
/*function object(o){
function F(){};
F.prototype = o;
return new F();
}
var person = {
name: 'Winnie',//基本类型值属性
friends: ['Shelby', 'Court', 'Van'] //引用类型值属性?共享了
};
var anotherPerson = object(person);
anotherPerson.name = 'Greg';
anotherPerson.friends.push('Rob');
console.log(anotherPerson.friends);//共享
var yetAnotherPerson = object(person);
yetAnotherPerson.name = 'Linda';
yetAnotherPerson.friends.push('Barbie');
console.log(yetAnotherPerson.friends);
console.log(person.friends);*/
//寄生式继承
/*
function object(o){
function F(){}
F.prototype = o;
return new F();
}
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
console.log('Hi');
};
return clone;
}
var person = {
name: 'Winnie',
friends: ['Shelby', 'Court', 'Van']
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();
function SuperType(name){
}*/