📜 ⬆️ ⬇️

Javascript Interview Questions

Not so long ago, I was puzzled by the search for a job, in connection with which I visited the n-th number of interviews and heard many interesting questions. There are a lot of articles on the network with questions on JS, so I will try to select questions that I have not yet seen. There are no questions of type What is a closure? , Inheritance in JavaScript or Make ajax request on VanillaJS . By the way, I advise you to look for answers to these questions before reading the article :) Under the cut questions like “with a trick”. It is unlikely that any of them will fall for you, but I hope the article will set you on a “dirty trick” thinking, and remind you of some slippery places and javascript underwater pebbles.


Hoisting


Hoisting is the ascent of variables declared in a function. Here you can learn in detail about how it happens.
But an interesting question:

(function() {
	f();

	f = function() {
		console.log(1);
	}
})()

function f() {
	console.log(2)
}

f();


?

gs function f() , f ReferenceError, - , , f 1.

:
< (function() {
  	f();

	f = function() {
		console.log(1);
	}
})()

function f() {
	console.log(2)
}

f();
> undefined
2
1



— . :

(function() {
	var x = 1;

	function x() {};
	
	console.log(x);	
})()


?

function declaration var. function x() {};, var x = 1;

< (function() {
var x = 1;

function x() {};

console.log(x);
})()

> undefined
1


. : JSLint.
var function expression



javascript :

var obj = {
	a: 1
};

(function(obj) {
	obj = {
		a: 2
	};

})(obj);

console.log(obj.a);


, . , .

!
obj . {a : 2}, obj, .




— , . . . .

Logger = function(logFn) {
	
	_logFn = logFn;

	this.log = function(message) {
		_logFn(new Date() + ": " + message);
	}
}

var logger = new Logger(console.log);

logger.log("Hi!");
logger.log("Wazzup?");


? ?

TypeError: Illegal invocation
logger.log(), — logger

.apply(), .call(), .bind()

< rightLogger = new Logger(console.log.bind(console))
> Logger {log: function}
< rightLogger.log("It's works")
> Sat Oct 04 2014 00:32:49 GMT+0400 (MSK): It's works  



UPD: 5

')

Source: https://habr.com/ru/post/239065/


All Articles