JavaScript interview questions and answers - 2
Blogs20142014-11-10
JavaScript interview questions and answers - 2
The following are Alex MacCaw’s questions, I answered good and now list in details.
(1)
spacify('hello world') // => 'h e l l o w o r l d'
function spacify(str) {
return str.split('').join(' ');
}
String.prototype.spacify = function(){
return this.split('').join(' ');
};
'hello world'.spacify();
(2)
function log(){
console.log.apply(console, arguments);
};
function log(){
var args = Array.prototype.slice.call(arguments);
args.unshift('(app)');
console.log.apply(console, args);
};
(3)
var User = {
count: 1,
getCount: function() {
return this.count;
}
};
console.log(User.getCount());
var func = User.getCount; //wrong
console.log(func()); //undefined
func.apply(User)
func.call(User);
var func = User.getCount.bind(User);
console.log(func());
Function.prototype.bind = Function.prototype.bind || function(context){
var self = this;
return function(){
return self.apply(context, arguments);
};
}