1 function foo() { 2 } 3 4 // foo.prototype.sayHello = function (a) { 5 // console.log(a) 6 // } 7 foo.prototype = { 8 func: function () { 9 console.log("func") 10 }, 11 sayHello: function (a) { 12 console.log(a) 13 } 14 }; 15 16 var p = new foo(); 17 p.sayHello("sayHello"); 18 p.func(); 19 20 21 var o = { 22 go: function () { 23 console.log("sayHello") 24 } 25 }; 26 var o1 = Object.create(o); 27 o1.go(); 28 29 var list = Object.create([]); 30 list.push("A"); 31 list.push("B", "C", "D", "E", "F", "G") 32 for (let i = 0; i < list.length; i++) { 33 console.log("" + i + "" + ": " + list[i]) 34 }
1 function foo() { 2 console.log("foo") 3 } 4 5 // 等价于 6 var func = Function("console.log("foo")"); 7 func() 8 9 var func = Function(foo()); 10 func() 11 12 function cmp(a, b, c) { 13 return Math.max(a, b, c) 14 } 15 16 var max = new Function('a', 'b', 'c', 'return cmp(a, b, c)') 17 console.log(max(11, 22, 33));
1 var a = eval("var num = 10;") 2 console.log(num); 3 4 var o = eval("({name:'kaka',age:19})") 5 console.log(o.name)
1 function foo() { 2 var num = Math.random(); 3 4 function func() { 5 return num; 6 } 7 8 return func; 9 10 } 11 12 var f = foo(); 13 var res1 = f(); 14 var res2 = f(); 15 16 console.log(res1); 17 console.log(res2); 18 19 20 21 function goo() { 22 var o = {name: "kaka"}; 23 return function () { 24 return o; 25 } 26 } 27 28 var g = goo(); 29 var ret = g(); 30 console.log(ret.name); 31 // 函数科里化(高阶函数) 32 console.log(goo()()); 33 34 35 36 function hoo() { 37 var num = Math.random(); 38 return { 39 get_num: function () { 40 return num; 41 }, 42 set_num: function (value) { 43 num = value; 44 } 45 } 46 } 47 48 var t = hoo(); 49 var num = t.get_num(); 50 console.log(num); 51 52 t.set_num(1); 53 num = t.get_num(); 54 console.log(num);
1 var str1 = '{ "name": "kaka", "sex": "man" }'; 2 3 var obj = JSON.parse(str1); //由JSON字符串转换为JSON对象 4 console.log(obj); 5 console.log(typeof (obj)); 6 7 var obj2 = eval('(' + str1 + ')'); //由JSON字符串转换为JSON对象 8 console.log(obj2); 9 console.log(typeof (obj2)); 10 11 var last = JSON.stringify(obj); //将JSON对象转化为JSON字符 12 console.log(last); 13 console.log(typeof (last));
1 class Demo { 2 constructor(a, b) { 3 this.a = a; 4 this.b = b; 5 return this; 6 } 7 8 print() { 9 console.log(this.a + ' ' + this.b); 10 } 11 }; 12 const demo = new Demo('hello', 'world').print(); 13 console.log(typeof demo);
1 class Point { 2 string(){ 3 return 'aaa' 4 } 5 } 6 class ColorPoint extends Point { 7 constructor(color) { 8 super(); // 调用父类的constructor() 9 this.color = color; 10 } 11 12 toString() { 13 return (this.string() ) 14 } 15 } 16 console.log(new ColorPoint('#000')); //console.log(new ColorPoint(1,2,'#000')) 17 console.log(new ColorPoint('#000').toString()); //aaa 18 19 console.log(new ColorPoint('#000').__proto__); //Point {constructor: function, toString: function} 20 console.log(ColorPoint.__proto__); // class Point{...} 21 console.log(ColorPoint.prototype); // Point {constructor: function, toString: function} 22 console.log(ColorPoint.__proto__.prototype); // Object {constructor: function, string: function}
1 function printThe() { 2 for (let i = 0; i < arguments.length; i++) { 3 return arguments 4 } 5 } 6 7 console.log(printThe(1, 2, 3, 4, 5, 6)); 8 9 const arr = [1, 2, 3, 4, 5, 6]; 10 11 function printTheTwo() { 12 arr.forEach((item, index) => { 13 console.log(item) 14 }) 15 } 16 17 printTheTwo(); 18 19 const obj = [{id: 1, name: "a"}, 20 {id: 2, name: "b"}, 21 {id: 3, name: "c"}, 22 {id: 4, name: "d"}]; 23 24 function printTheThree() { 25 obj.forEach((item, index) => { 26 console.log(item) 27 }) 28 } 29 30 printTheThree()