Objects and OOP Flashcards

1
Q
If you call a constructor function without using 'new', what happens? e.g.
var tom = Person( "Tom" ); // instead of 
var jim = new Person( "Jim" );
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

False, because each instance gets its own function instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
function Obj() {};
Obj.prototype.sayName = function() {};
obj1.sayName == obj2.sayName; // true or false?
A

True. Instances all share a reference to the same prototype object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do the constructor, prototype, and instances refer to one another in the prototype pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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”;

A

The instance property blocks access to the prototype property. This can be undone by saying,
delete person1.name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are two ways to use the “in” operator?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the hasOwnProperty(arg) do and how?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you tell if an object has access to a property not directly, but through prototype?

A

myObject.hasOwnProperty(“name”) == false && “name” in myObject == true;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the keys() do with objects and how?

A

Object.keys(myObject) returns an array of all enumerable properties of an object instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How could you tell if SuperObject is a prototype of SomeSubClass?

A

SuperObject.prototype.isPrototypeOf(subClassInstance); // needs .prototype in there!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Constructor Stealing?

A
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 );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a lazy function?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you protect against the possibility of forgetting to add “new” when creating a new object using constructor method?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does getOwnPropertyNames( arg ) do and how?

A

Object.getOwnPropertyNames(myObj) returns an array of all instance properties of an object whether enumerable or not (but not from the prototype).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between arguments.length and someFunction.length ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does a for … in loop do with an object?

A

A for…in loop lists all enumerable properties in both the instance and the prototype chain.

17
Q

What are two reasons to use bracket notation with an object?

A
  1. To access a property using a variable (e.g. myVar = “name”; Now you can say: myObj[someVar] rather than myObj[“name”];
  2. To access a property with a syntactically bad name like myObj[“this is a name”]; (since it has spaces)
18
Q

What does Object.defineProperty() do?

A

It sets whether a given property of an object is enumerable, configurable or writable; its value; and its getter/setter functions.