This is a topic that I like to give anyone interviewing for a JS position at my company. It's a bit of a brain-twister, and unless you really understand how prototypal inheritance works in JS, there's a pretty good chance you'll get it wrong.
What's the difference between:
1. var objB = Object.create(objA);
and
2. var objB = new objA();
When we write #1, objB's internal [[proto]] property points to the object that is objA. That means that when we execute something like:
var objA = {
a:42;
}
var objB = Object.create(objA); //objB.__proto__ === objA
console.log(objB.a);
However, when we have the following scenario, invoking a function as a constructor:
function objA(){
this.a = 42;
}
objA.prototype.b = 43;
var objB = new objA(); //objB.__proto__ === objA.prototype
console.log(objB.a); //42
console.log(objB.b); //43
console.log(objB.__proto__); //objA.prototype
So we see that when when use the new keyword with a function, the object that is created (objB) has it's internal [[proto]] pointing to the function's prototype property (objA.prototype).
A blog about Javascript. For topics on security and hacking, please see my security blog at http://guerresanslarmes.blogspot.com/
Saturday, June 4, 2016
Talking DOMball...
Hello all you baseball fans and JS wahoos, it's time for another season of baseball!!
It's also time for us to discuss the differences between two similar sounding words in Javascript, document, and window.
window is the global object in Javascript. It holds everything, like global variables, global functions, history, location, etc. Global functions, such as setTimeout and console, live on the window as well. Even document lives on the window.
console.log(window.setTimeout === setTimeout); // true
console.log(window.document === document); //true
window can also be thought of as the default scope or this keybinding in browser-based JS (environments like nodejs do not have a window object).
var x = 5; //x in global/window scope
console.log(this.x === window.x); //true
console.log(this === window); //true
document is the DOM. The DOM is a tree of nodes (html tags, like h1, div, span, etc), and may be thought of as an object-oriented representation of the HTML that makes up the page. For an example of what this tree looks like, check out this site:
https://gojs.net/latest/samples/DOMTree.html
document is immensely useful because it allows us to query and manipulate the DOM using Javascript. For example, to grab all h1 elements in the DOM, we would write:
var headers = document.getElementsByTagName('h1');
Check back later, when we'll continue our exploration of window and document.
It's also time for us to discuss the differences between two similar sounding words in Javascript, document, and window.
window is the global object in Javascript. It holds everything, like global variables, global functions, history, location, etc. Global functions, such as setTimeout and console, live on the window as well. Even document lives on the window.
console.log(window.setTimeout === setTimeout); // true
console.log(window.document === document); //true
window can also be thought of as the default scope or this keybinding in browser-based JS (environments like nodejs do not have a window object).
var x = 5; //x in global/window scope
console.log(this.x === window.x); //true
console.log(this === window); //true
document is the DOM. The DOM is a tree of nodes (html tags, like h1, div, span, etc), and may be thought of as an object-oriented representation of the HTML that makes up the page. For an example of what this tree looks like, check out this site:
https://gojs.net/latest/samples/DOMTree.html
document is immensely useful because it allows us to query and manipulate the DOM using Javascript. For example, to grab all h1 elements in the DOM, we would write:
var headers = document.getElementsByTagName('h1');
Check back later, when we'll continue our exploration of window and document.
Subscribe to:
Posts (Atom)