JS-練習題

一、foo()結果數組

1      function foo() {
2             bar.apply(null, arguments);
3         }
4         function bar(){
5             console.log(arguments);
6         }
7 
8         foo(1,2,3,4,5);
結果:(1,2,3,4,5)

二、alert的結果app

1 function b(x, y, a){
2             arguments[2] = 10;
3             alert(a);
4         }
5         b(1, 2, 3);
結果:10

三、typeof fthis

1 var f = (
2             function f(){
3                 return "1";
4             },
5             function g(){
6                 return 2;
7             }
8         )();
9         typeof f;
結果;"number"
逗號運算符:(x,y),輸入結果是:兩個值進行比較,誰大輸出誰

四、xspa

1 var x = 1;
2         if (function f(){}) {
3             x += typeof f;
4         }
5 console.log(x);
結果:"1undefined"

五、求100的階乘prototype

 var num = (function(n){
            if (n == 1){
                return 1;
            }
            return n * arguments.callee(n-1);
        }(100))

六、thiscode

1 var foo = '123';
2         function print(){
3             var foo = '456';
4             this.foo = '789';
5             console.log(foo);
6         }
7         print();
結果:456
1  var foo = '123';
2         function print(){
3             this.foo = '789';
4             console.log(foo);
5         }
6         print();
結果:789
1  var foo = '123';
2         function print(){
3             this.foo = '789';
4             console.log(foo);
5         }
6    new print();
結果:123

七、print()()blog

 1    var bar = {a: "002"};
 2         function print(){
 3             bar.a = 'a';
 4             Object.prototype.b = 'b';
 5             return function inner() {
 6                 console.log(bar.a);
 7                 console.log(bar.b);
 8             }
 9         }
10         print()();
結果:a   
     b

 八、深拷貝索引

 1    var bar = {a: "002"};
 2         function print(){
 3             bar.a = 'a';
 4             Object.prototype.b = 'b';
 5             return function inner() {
 6                 console.log(bar.a);
 7                 console.log(bar.b);
 8             }
 9         }
10         print()();

九、類數組get

 1 var obj = {
 2             "2" : "a",
 3             "3" : "b",
 4             "length" : 2,
 5             "push" : Array.prototype.push
 6         }
 7         obj.push("c");
 8         obj.push("d");
 9 
10 //obj爲?
結果:{2: "c", 3: "d", length: 4}
解析:類屬性
        屬性要爲索引,必需要有length,最好有push

        Array.prototype.push = function(target) {
             obj[obj.length] = target;
             obj.length++;
        }
相關文章
相關標籤/搜索