QUESTION:
True or False: the terms “scope” and “context” refer to the same thing in JavaScript.
Click for Answer
False
Explanation:
Scope pertains to the visibility of variables and in contrast, context refers to the object to which a method belongs (which can be changed by using call or apply).
QUESTION:
If you omit the “var” keyword when creating a variable in a function, it becomes a property of what object?
Click for Answer
Explanation:
When omitting the “var” keyword, the variable you create becomes an “implied global”. However, implied globals are not variables in a function. As a result of this mistake, an implied global actually becomes a property of the window object. Although the window object is considered the “global” scope, it is an object, and any variable declared in the global scope (intentionally or otherwise), actually becomes a property of the window object.
QUESTION:
How do you determine if a JavaScript instance object was created from a specific constructor or not?
Click for Answer
QUESTION:
True or False: Once you create an object, you can add, remove or change properties of that object at any time.
Click for Answer
QUESTION:
True or False: You can only instantiate a JavaScript constructor function once.
Click for Answer
Explanation:
You can make as many instances as you want. There is no limit.
QUESTION:
When you create a function inside of a method, what does the “this” keyword refer to when used in that function?
Click for Answer
The “window” object
Explanation:
Inside of a function, the “this” keyword always refers to the window object. On the other hand, when a function is a property of an object, the “this” keyword always refers to the object to which that method belongs.
Example:
|
|
var myObject = { color : "red", test : function(){ function testThis(){ console.log(this.color); //undefined console.log(window === this); //true }; testThis(); console.log(this.color); //red console.log(window === this); //false return "done!" } }; myObject.test(); |
In the above example, the output to your JavaScript console should be:
- undefined
- true
- red
- false
- “done!”
The reason for the output is: The testThis function inside of the myObject.test method attempts to log the value of “this.color“. However, that function is NOT a method, it is just a function declaration, thus “this” refers to the window object. In this code, the window object, does not have a “color” property, and accordingly “this.color” is undefined. Next, the testThis function inside of the myObject.test method compares, window to the “this” keyword (just to prove the previous point). Again, because the testThis function inside of the myObject.test method is NOT a method, it is a property of the window object, and therefore “this” DOES equal the window object.
Next, the myObject.test method attempts to log the value of “this.color”. Since the test metod is a property of the myObject object, and the myObject object has a property named “color”, the value of that property is logged to the console. Next, the myObject.test method attempts to compares window to the “this” keyword. Since the test metod is a property of the myObject object, the “this” keywork refers to the myObject object, and the comparison to the window object returns false.
QUESTION:
Name two ways two change the context of a JavaScript method
Click for Answer
Use the call or apply methods
Explanation:
The call and apply methods can be used to specify the context of another method.
|
|
var objectA = { name : "I am objectA", sayName : function(){ alert("I am: " + this.name); } }, objectB = { name : "objectB", sayName : function(){ alert("I am: " + this.name); } }; objectA.sayName.call(objectB); //alerts: "I am ObjectB" objectB.sayName.apply(objectA); //alerts: "I am ObjectA" |
QUESTION:
Can a JavaScript constructor return a primitive value (e.g. a number or a string)?
Click for Answer
Explanation:
A JavaScript constructor can only return an object. When no return value is specified, it returns an instance of itself. However, if an object is specified as the return value, then that object is the return value. Thus, if any value other than an object is specified as the return value, then it returns an instance of itself.
QUESTION:
In JavaScript, are objects passed by reference or by value?
Click for Answer
In JavaScript, all objects are passed by reference. When you make a change to a reference to an object, you change the actual object. Primitive types are however, passed by value.
Explanation:
Here is a jsFiddle.net example: http://jsfiddle.net/pmMLc/
QUESTION:
What is the difference between a constructor function and a non-constructor function with respect to the word “this”
Click for Answer
QUESTION:
What is the name of the property that allows you to add properties and methods to an object, as well as every object that inherits from it?
Click for Answer
QUESTION:
True or False: An object literal can be used to create private variables.
Click for Answer
False. Only functions can be used in JavaScript to create privacy
Explanation:
All of the properties of a object literal are public and can be easily mutated. The only way to create private variable in JavaScript is by using a function. You can make a property of an object literal a function (i.e. a “method”), and use private variables in that function, but the named property will always be public.
QUESTION:
What is the name of the object that refers to the application used to view a web page?
Click for Answer
QUESTION:
True or False: The object returned by the document.getElementsByTagName() method is an array.
Click for Answer
QUESTION:
Which object.property combination provides a reference to the protocol used to view the current web page?
Click for Answer
QUESTION:
Given the below code, what line of JavaScript would allow foo.getColor to return the value of foo.color, without using the word “foo”?
|
|
var foo = { color : "red", getColor : function(){ //make this function return foo.color //without using the word: "foo" } } |
Click for Answer (+)
return this.color
Explanation:
When a function is a property of an object, it is called a “method”. Inside of a method, the JavaScript “this” keyword refers to the object to which the method belongs. Therefore, in foo’s getColor method, if you return “this.color”, you will thus return the value of foo’s “color” method. A key point is: the advantage to this over “return foo.color” is that when using “this” instead of “foo”, the code in the getColor method is re-usable.
QUESTION:
What is the difference between using dot notation and bracket notation when accessing an object’s property?
Click for Answer (+)
If using dot notation, the property must be a string and refer to a property that in fact exists.
When using bracket notation, any valid JavaScript expression that produces a value can be used inside the brackets, such as a variable or an an array element.
Explanation:
These are all valid lines of code:
|
|
foo = bar.name; foo = bar["name"]; foo = bar[5 + 5]; foo = bar[ baz() ]; foo = bar[ baz[i] ]; |
QUESTION:
What is important to remember about the last property’s value in a JavaScript object literal?
Click for Answer
QUESTION:
Given the following code, what is very likely the reason that the programmer made the first letter of “Foo” a capital letter?
Click for Answer
Foo is meant to be used as a constructor function.
Explanation:
In spite of the fact there is no technical requirement that constructor function names begin with a capital letter, it is a common convention and a pattern that most experienced JavaScript developers recognize.
QUESTION:
Given the following code, how would you instantiate the function “Foo” and assign it to the variable “bar”?
Click for Answer
QUESTION:
What are two ways in which the variable “foo” can be assigned to an empty object?
Click for Answer
- var foo = new Object();
- var foo = {};
Explanation:
When creating a new empty object, you can instantiate the Object() constructor, or you can simply create an empty object literal. For this reason, in either case, you can then add properties to the new object.
Here is a jsFiddle.net example: http://jsfiddle.net/W6X2T/
QUESTION:
True or False: When you create an object literal, you must first instantiate the Object() constructor.
Click for Answer
Explanation:
In order to create an Object Literal, you assign a variable to an object. As a matter of fact, the syntax is as simple as var foo = {}. However, a constructor function is used when creating an instance object, which is a different process.
QUESTION:
When using the addEventListener() method to create a click-handler for a DOM element, what is the value of “this” inside of the callback you specify?.
Click for Answer
QUESTION:
True or False: A JavaScript array is not an object
Click for Answer
QUESTION:
If a string is a primitive value, why does it have a split() method?
Click for Answer
QUESTION:
True or False: A function can have its own properties and methods.
Click for Answer
True
Explanation:
Functions as a matter of fact, inherit from Object. Therefore, you can add properties and methods to a function at any time. Run the following code in your JavaScript console and then you will see this behavior:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//create a function function foo(){}; //assign a property foo.color = 'red'; //assign a method foo.sayHello = function(){ alert("hello!"); }; //inspect the object console.dir(foo); //inspect the properties and methods of off //execute the method foo.sayHello();// "hello!" //overwrite a method foo.sayHello = function(){ alert("this is a different message"); }; //execute the method foo.sayHello();// "his is a different message" |