Js面试题

1.

window.val = 1;
var json = {
    val:10,
    a:function(){
          this.val *= 2;
          return this.val;
     }
}
console.log(json.a());  
var a = json.a;    
console.log(a());    
json.a.call(window);   
console.log(json.a());    
console.log(window.a()) ;

2.

function C1(name){
    if(name) this.name = name;
}
function C2(name){
    this.name = name;
}
function C3(name){
    this.name = name || 'nana';
}
C1.prototype.name = 'TOM';
C2.prototype.name = 'TOM';
C3.prototype.name = 'TOM';
console.log((new C1().name) + (new C2().name) +(new C3().name));

3.

function A(x){
    this.x = x;
}
A.prototype.x = 1;
function B(x){
    this.x =x; 
}
B.prototype = new A();
var a = new A(2);
var b = new B(3);
delete b.x;
console.log(a.x); 
console.log(b.x);

4.

var   hero = {
    _name:'John Doe',
    getSecretIdentity:function(){
        return   this._name;
    }
};
var   stoleSecretIdentity = hero.getSecretIdentity;
console.log(stoleSecretIdentity());  
console.log(hero.getSecretIdentity());

5.

(function(x) {
     return(function(y) {
         console.log(x);
     })(2)
})(1);

6.

for(var i = 0; i < 5; i++) {
    setTimeout(
        function(){console.log(i); 
    }, i * 1000 );
}

7

console.log('begin')
for(var i = 0; i < 10; i++) {
    setTimeout(
        function(){console.log(i); 
    }, i * 1000 );
}
console.log('end');

8.

console.log(1 +"2"+"2");    
console.log(1 +  +"2"+"2"); 
console.log(1 +  -"1"+"2"); 
console.log("A"-"B"+"2");   
console.log("A"-"B"+ 2);

9.

    var b = 1;
    function outer(){
        var b = 2
        function inner(){
            b++;
            var b = 3;
            console.log(b)
        }
        inner();
    }
    outer();

10

console.log(typeof typeof 1);

11

console.log(1 < 2 < 3); 
console.log(3 > 2 > 1); 

12 如何克隆一个对象

var obj1 = { a: 10, b: 20, c: 30 };
var obj2 = Object.assign({}, obj1);
obj2.b = 100;
console.log(obj1);
console.log(obj2);

13

(function () {
    try {
        throw new Error();
    } catch (x) {
        var x = 1, y = 2;
         console.log(x);
    }
    console.log(x);
    console.log(y);
})();

14

(function(){
          var a = b = 3;   
})();
console.log("a defined? " + (typeof a !== 'undefined'));   
console.log("b defined? " + (typeof b !== 'undefined')); 

15

var myObject = {
    foo: "bar",
    func: function() {
         var self = this;
         console.log("outer func: this.foo = " + this.foo);   
         console.log("outer func: self.foo = " + self.foo);   
         (function() {
                    console.log("inner func: this.foo = " + this.foo);  
                    console.log("inner func: self.foo = " + self.foo); 
         }());
    }
};
myObject.func()

16

function foo1(){
    return {
        bar: "hello"
    };
}
function foo2(){
    return 
    {
        bar: "hello"
    };
}
console.log(foo1().bar);
console.log(foo2().bar);

17

var a = {},
b = {key:'b'},
c = {key:'c'};
a[b]=123;
a[c]=456;
console.log(a[b]);

18

console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(10));

19

var name = 1;
var obj = {
  name:2,
  sayName:function(){
    function s1(){
      console.log(this.name);
    }
    var s2 = () => {console.log(this.name)}
    console.log(this.name)
    s1()
    s2()
  }
}
function sayName(){
   console.log(this.name)
}
obj.sayName()
sayName.call(obj);
var fn = obj.sayName
fn()

20

typeof undefined == typeof NULL

21

var length = 10;
function fn() {
    console.log(this.length);
}
var obj = {
    length: 5,
    method: function(fn) {
        fn();
        arguments[0]();
     }
};
obj.method(fn, 1);

22

function Foo() {
    getName = function () { console.log (1); };
    return this;
}
Foo.getName = function () { 
    console.log (2);
};
Foo.prototype.getName = function () { 
    console.log (3);
};
var getName = function () { 
    console.log (4);
};
function getName() {
    console.log (5);
}
//请写出以下输出结果:
    Foo.getName();  
    getName();     
    Foo().getName(); 
    getName();      
    new Foo.getName(); 
    new Foo().getName();
    new new Foo().getName(); 

23

console.log(typeof y)

24

    getName()   
    var getName = function() {
        console.log('wscat')
    }
    getName()  
    function getName() {
        console.log('oaoafly')
    }
    getName() 

25

var Employee = {
     company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);

26

var output = (function(x){
        delete x;
        return x;
})(0);
console.log(output); 

27 怎么判断一个object是否是数组(array)?

var obj = {};
var arr = [];
obj.constructor == Object;
arr.constructor == Array

28

var aaa = (function(){
        var a = 1;
        function bbb(){
            a++;
            alert(a);
        }
        function ccc(){
            a++;
            alert(a);
        }
        return {
            b:bbb,
            c:ccc
        }
})();
aaa.b();
aaa.c()

29

function outer(){
        var x=10;
        return function(){
            x++;
            alert(x);
        }
}
var y = outer();
y();
y(); 

30

var a = 1;
function abc(){
        a++;
        console.log(a);
}
abc(); 
abc(); 

31

(function(){
        var val = 1;
        var json = {
            val:10,
            a:function(){
                val *= 2;
            }
        };
        json.a();  
        console.log(json.val + val)   
}())

32将此类型的字符串'name=1&value=2&key=3'转为对象形式{name:1,value:2,key:3}

var str = "name=1&value=2&key=3";
var arr = str.split('&');
var obj = {};
for(var i = 0 ;i < arr.length;i++){
    obj[arr[i].split('=')[0]]=arr[i].split('=')[1];
}

你可能感兴趣的:(Js面试题)