If you call a constructor function without using 'new', what happens? e.g. var tom = Person( "Tom" ); // instead of var jim = new Person( "Jim" );
Any properties or methods set inside the Person() function will belong to the Global/window object (e.g. this.name will refer to window.name). Any properties or methods of Person.prototype will not be related.
If an object made using the constructor pattern has a function, sayName(), inside the constructor, will this be true or false?
obj1.sayName == obj2.sayName
False, because each instance gets its own function instance.
function Obj() {};
Obj.prototype.sayName = function() {};
obj1.sayName == obj2.sayName; // true or false?True. Instances all share a reference to the same prototype object.
How do the constructor, prototype, and instances refer to one another in the prototype pattern?
Constructor has a property called prototype, which is an object. Prototype has a property called constructor that points back at the constructor. Each instance has a property called prototype which points to the prototype object.
If an instance has a property of the same name as a property on that instance’s prototype, what happens?
Person.prototype.name = “Henry”;
person1.name = “Bill”;
The instance property blocks access to the prototype property. This can be undone by saying,
delete person1.name;
What are two ways to use the “in” operator?
In a for/in loop and on it’s own. On its own, it tells whether a property is available to an object, whether directly or through prototype or inheritance: “name” in person (use quotes)
What does the hasOwnProperty(arg) do and how?
someObj.hasOwnProperty(“propName”) returns true if that property has been set on the instance; false if it doesn’t exist or is a prototype property.
How can you tell if an object has access to a property not directly, but through prototype?
myObject.hasOwnProperty(“name”) == false && “name” in myObject == true;
What does the keys() do with objects and how?
Object.keys(myObject) returns an array of all enumerable properties of an object instance.
How could you tell if SuperObject is a prototype of SomeSubClass?
SuperObject.prototype.isPrototypeOf(subClassInstance); // needs .prototype in there!
What is Constructor Stealing?
It's a way to handle a problem (of reference values) with prototype chaining by making the subtype call the supertype constructor:
function SubType( ) {
SuperType.call( this );What is a lazy function?
It's a way to reduce 'if' statements for performance.
var myFunc = function( ){ if ( ... ) { myFunc = function( ) { // do one thing; };
} else { myFunc = function( ) { // do another; }; }
return myFunc( );How can you protect against the possibility of forgetting to add “new” when creating a new object using constructor method?
Scope-safe constructor: in constructor, check if, e.g., this instanceof Person. If not, return new. e.g.: return new Person ( ); (This can be “tricked” with apply or call).
What does getOwnPropertyNames( arg ) do and how?
Object.getOwnPropertyNames(myObj) returns an array of all instance properties of an object whether enumerable or not (but not from the prototype).
What is the difference between arguments.length and someFunction.length ?
arguments.length is local to a function and gives the number of arguments received by a given function call.
someFunction.length gives the number of arguments a function expects
What does a for … in loop do with an object?
A for…in loop lists all enumerable properties in both the instance and the prototype chain.
What are two reasons to use bracket notation with an object?
What does Object.defineProperty() do?
It sets whether a given property of an object is enumerable, configurable or writable; its value; and its getter/setter functions.