Prototypes

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Inside a class (function) What does this.name = name do?

It adds the .name property onto each object (using this binding).

What are three scenarios for when a property = assignment is taking place on a lower-level obtain but the property doesn't exist on that object, but IS at a higher level of the objects prototype chain?

1. If a normal data accessor property name foo is found anywhere higher on the [[prototype]], and it's not marked as read-only (writable: false) then a new property called foo is added directly to the lower level object, resulting in a shadowed property. 2. If a foo is found higher on the [[prototype]] chain, but it's marked as read-only (writable:false), then both the setting of that existing property as well as the creation of the shadowed property on the lower level object are disallowed. If the code is running in strict mode, an error will be thrown. Otherwise, the setting of the property value will silently be ignored. Either way, no shadowing occurs. 3. If a foo is found higher on the [[prototype]] chain, but it's a setter, then the setter will always be called. No foo will be added to (aka shadowed on) the lower object, nor will foo setter be redefined.

What prevents the presence of a property of the same name from being created (shadowed) at a lower level of a prototype chain?

A read-only property descriptor (writable:false)

In JavaScript, what is a constructor?

Any function called with the new keyword in front of it. Functions are NOT constructors, however function calls are "constructor calls" if and only if 'new' is used.

How can you add a shadow for a property even when the property is both 2. read-only (writable:false) and 3. Has an already defined setter (which cannot be redefined) based on assignment.

If you want to shadow a property in cases #2 and #3, you cannot use = assignment, but must instead use Object.defineProperty(...) to add the property to the lower object.

What's it called when you have an object, and want to find out what object (if any) it delegates to?

Inspecting an instance (just an object in JS) for its inheritance ancestry (delegation linkage in JS) is often called introspection (or reflection) in traditional class-oriented environments.

What is instanceOf operator?

It's a way we can introspect an object to find its "ancestry" (delegation linkage). The instanceOf operator takes a plain object as its left-hand operand and a function as it's right-hand operand. The question instanceOf answers is: In the entire [[prototype]] chain of an object, does the object arbitrarily pointed to by the function.prototype ever appear? Unfortunately, this means that you can only inquire about the "ancestry" of some object if you have a function (with its attached prototype reference) to test with. If you have two objects, and want to find out if the object are related to each other through a [[prototype]] chain, instanceof alone cannot help.

In Javascript, can classes be made from the process of instantiating (inheriting from) a class means?

No, in Javascript, there are no such copy-actions performed. You don't create multiple instances of a class. You can create multiple objects that [[prototype]] link to a common object. By default, no copying occurs, and thus these object don't end up totally separate and disconnected from each other, but rather quite linked.

Does the below create a new instance? var a = new Foo();

No, it caused two objects to be linked to each other via [[prototype]]. (Foo.prototype) object. It created a new object thats linked to a super object.

Is it safe to rely on the .constructor to reference the object higher in the prototype chain?

No, it's an unsafe reference to reply upon in your code.

Is shadowing something that is encouraged?

No, one should try and avoid it because it causes complication when delegation is needed between them. Shadowing with methods leads to ugly explicit-polymorphism.

Does "constructor" mean "was constructed by"

No, this is a misconception by a lot of developers. it's just a property in every function that could be overwritten. Therefore it could accidentally point to the Object.prototype.

Is .constructor enumerable?

No.

Is the .constructor immutable?

No.

What does Object.create() do?

Object.create(..) creates an object with the [[prototype]] linkage we're examining to the object specified. var sourceObject = {a:2}; example: var targetObject = Object.create(sourceObject) targetObject.a // 2

Whats the top of the delegation chain of prototype?

Object.prototype This has a .constructor on it which points to the built-in Object(...) function.

What does ES6's Object.setPrototypeOf(....) do?

Object.setPrototypeOf(..) modifies existing prototype with another. Object.setPrototype(targetObject.prototype, sourceObject.prototype)

[[Prototype]]

Objects in Javascript have an internal property, denoted in the specification as [[prototype]], which is simply a reference to another object. Almost all object are given a non-null value for this property, at the time of their creation.

How can one change a property in a higher object in the prototype chain thats being shadowed?

One would have to directly change the value on the higher object. Example higherObject.propertyA++ if one changes the lower object's property in hopes to change the higher one, it will change the lower one only. The result is [[Get]] looking up the property via prototype to get the current value from the higher object's property, and incrementing the value by one, then [[put]] assigning the new value to a new shadowed property on the lower object.

What objects that are created won't have the constructor property?

Ones that are created via Object.create(null)

What is prototypal inheritance?

Prototypal inheritance is commonly said to be the dynamic-language version of "classical inheritance". It's an attempt to piggy-back on the common understanding of what "inheritance" mean in the class-oriented world, but tweak the understood semantics, to fit dynamic scripting. "Inheritance" implies a copy operation, and javascript doesn't copy object properties (natively, by default). Instead, JS creates a link between two objects, where one object can essentially delegate property/function access to another object. "Delegation" is a much more accurate term for JavaScripts object-linking mechanism.

How can we create a new object prototype that's properly linked to another object's prototype?

SourceObject.prototype = Object.create(sourceObject.prototype)

What does a capital lettered function tell you?

That this function is meant to be a class. 'Foo' instead of 'foo'.

What does the .constructor property do?

The .constructor property points to a property located on Foo.protoype that points to itself (Foo). This is helpful for wanting to know if a lower object in the prototype chain is connected to a super() object, however, there are complications that can arise. 1. If a Foo.prototype gets replaced with another object (which doesn't have the default properties that it's usually given).

What happens when a property is never found on a prototype chain?

The [[Get]] operation returns undefined.

When a property is looked up on an object, and it's not found directly in the object itself, where does it look next?

The default [[Get]] operation proceeds to follow the [[prototype]] link of the object if it cannot find the requested property on the object directly.

How is a for...in loop similar to the prototypes chain look-up process?

The for..in loop iterates over any property that can be reached in the prototype chain, however, all properties have to have enumerable: true. However if you use the in operator to check for existence of a property on an object, 'in' will check the entire chain of the object (regardless of enumerability).

What is differential inheritance?

The idea that we describe an object's behavior in terms of what is different from a more general descriptor. Just like with "Prototypal inheritance", "differential inheritance" pretends that your mental model is more important than what is physically happening in the language. It overlooks that object b is not actually differentially constructed, but is instead built with specific characteristics defined, alongside "holes" where nothing is defined. It is in these "holes" (gaps in, or lack of, definition) that delegation can take over and, on the fly, "fill them in" with delegated behavior. The object is NOT, by native default, flattened into the single differential object, through copying, that the mental model of differential inheritance implies. "differential inheritance" is just not as natural a fit for describing how Javascript's [[prototype]] mechanism actually works.

When a property is shadowed which property name takes priority?

The lowest property name on the prototype chain, therefore the prototype chain doesn't need to be examined since the name is present on the current object.

Where does the [[prototype]] chain end?

The top-end of every normal [[prototype]] chain is the built in Object.prototype This object includes a variety of common utilities used all over JS, because all normal (built-in host-specific extension) object in JS "descend from" (aka, have at the top of their prototype chain) the Object.prototype object.

What does Foo.prototype.myName = function() { return this.name;} do?

This adds a property (function) to the Foo.prototype object. Now, a.myName works because when the default [[Get]] doesn't find the property directly on the lower objects, it traverses up the prototype chain to find the fall-back reference on the Foo.prototype object. Since we used the "new" keyword this attached the prototype linkage to the lower objects.

What does traverse/traversing mean?

To travel across or through. Therefore it's common to say the prototype chain is traversed.

What is shadowing?

When a property name is on both an object and a higher level on the prototype chain. The lower object takes priority over the "shadowed" higher prototype chained property.

What is the only scenario where a property will be shadowed in the prototype chain?

When the higher property isn't set to read-only. (writable:false).

What creates a constructor call?

When we put the "new" keyword in front of a normal function call. "new" sort of hijacks any normal function and calls it in a fashion that constructs an object, in addition to whatever else it was going to do.

Do functions have a prototype?

Yes, all functions have by default get a public, non-enumerable property on them called prototype, which points at an otherwise arbitrary object. This object holds properties such as .constructor

Is it possible for an object to have an empty prototype linkage?

Yes, however this is somewhat less common.

Does the ES6 'proxy' effect the default behavior of the [[Get]] operation behavior?

Yes. However, we will learn more about proxy later.

Can an object be created directly without a class at all?

Yes. JS is almost unique among languages as perhaps the only language with the right to use the label "object oriented", because its one of a very short list of languages where an object can be created directly, without a class at all. In JavaScript, class can't (being that they don't exist) describe what an object can do. The object defines its own behavior directly. There's just the object.

How can one manually add a constructor to a new object that points to the class (function)?

You can use the Object.defineProperty( Foo.prototype, "constructor", { writable:true, enumerable: false, configurable: true, value: Foo //point to Foo } );


Ensembles d'études connexes

Data Science Foundations: Data Mining

View Set

Psychology the study of mental processes and behavior

View Set

Good Manufacturing Practices (GMP)

View Set