• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • JavaScript interview questions and answers - 1

    Blogs20142014-11-10


    JavaScript interview questions and answers -1

    (1) write a bind function, to make a function call other function with its parameters:
    Function.prototype.bind = Function.prototype.bind || function(context){
      var self = this;
    
      return function(){
        return self.apply(context, arguments);
      };
    }
    
    function Foo(data1, data2) {
      this.x = data1;
      this.y = data2;
      console.log('Foo', this.x, this.y);
    };
    
    var bar = {};
    var data1='123456', data2 = { a:'b', c:'d' };
    
    Foo.bind(bar)(data1, data2); //same: bar = new Foo(data1, data2);
    bar; //Object {x: "123456", y:{ a:'b', c:'d' }}
    
    
    (2) what's wrong with the following function, and how to improve it?
      for(var i=0; i<3; i++) {
        setTimeout(function() {
    	   console.log(i);
    	}, 1000);
      }
      // wrong return: 3 3 3
      // improve:
      for(var i=0; i "David", "Accounts"
    function Manager() {
        this.name_ary = [];
        Employee.apply(this, arguments);
    }
    
    Manager.prototype = Employee.prototype;
    Manager.prototype.getDirectReports  = function() {
        return this.name_ary.join(', ');
    }
    Manager.prototype.setDirectReposts = function () {
       this.name_ary = Array.prototype.slice.call(arguments);
    }
    
    var david= new Manager( "David", "Accounts");
    david.getInfo(); // Accounts David
    
    david.setDirectReposts('Vijay', 'William');
    console.log(david.getDirectReports()); // Vijay, William
    
    (7) other solution:
    function Manager() {
        this.name_ary = [];
    	Employee.apply(this, arguments);
    }
    
    Manager.prototype = new Employee();
    Manager.prototype.constructor=Manager;
    Manager.prototype.getDirectReports  = function() {
        return this.name_ary.join(', ');
    }
    Manager.prototype.setDirectReposts = function () {
       this.name_ary = Array.prototype.slice.call(arguments);
    }
    
    var david= new Manager( "David", "Accounts");
    david.getInfo(); // Accounts David
    
    david.setDirectReposts('Vijay', 'William');
    console.log(david.getDirectReports()); // Vijay, William
    
    
    
    (8) sort a array of hash.
    // Array of objects.  [{name:'vijay', id: '10'}, ....]
    
    // Sort the array by name.
    var arr =  [{name:'ben', id: '1'}, {name:'vijay', id: '10'}, {name:'bill', id: '1'}, {name:'william', id: 20}];
    arr.sort(function(a,b) {
         if ( a.name  b.name )
           return 1;
         return 0;
     } );
    console.log(arr); // sequence: ben,bill,vijay,william
    
    
    (9) curry
    var paths=['abc','efg','zzz','xxx'];
    function simpleURL(protocol, domain, path) { return protocol + '://' + domain + '/' + path; }
    var siteDomain='dixitruth.com'
    paths.map(simpleURL.bind(null, 'http', siteDomain));
    ["http://dixitruth.com/abc", "http://dixitruth.com/efg", "http://dixitruth.com/zzz", "http://dixitruth.com/xxx"]
    
    (10)
    var one = {
      name: 'object',
      say: function(greet) {
        return greet + ', ' + this.name;
      }
    };
    var two = { name: 'another object' };
    one.say.call(two, 'hello'); //"hello, another object"