Can you convert a superclass reference into a subclass reference? A subclass reference into a superclass reference? If so, give examples. If not, explain why not
JavaScript doesn't have typed references at all. An object reference is just an object reference, the full and complete extent of any type information it has is that it's an object reference. So there's no need to convert a reference from a superclass reference to a subclass reference.
The object itself (not the reference) does have some information that, in some ways, links it back to its "class" (which is to say, its constructor function), indirectly via its prototype — assuming that A) The prototype isn't changed via Reflect.setPrototypeOf (a reasonable assumption, but there's no guarantee), and B) the prototype property on the constructor function doesn't get changed (also frequently a reasonable assumption, but not a guarantee, once the codebase is done initializing [and not until then]). For instance, if an object is created via a constructor function, in the normal case it inherits a constructor property from its prototype which, in the normal case, points back at the constructor function. You can't really rely on it, because if someone has modified the object referenced by the constructor function's prototype property it may not have a constructor property at all (and thus inherits it from its prototype) or may have one pointing at the wrong function.
It's important to remember that JavaScript is a prototypical language, not a class-based one. The new class semantics in ES6 are almost entirely syntactic sugar for setting up chains of constructors and their prototypes. ("Almost" because there's some plumbing related to subclassing builtins like arrays that we couldn't do previously.) One aspect of JavaSCript not being class-based is that questions like this don't really relate to it.
Related
Is it true to say that all objects (besides some custom objects), has a prototype object, in JavaScript?
I would bet it is indeed this way (because all out of the box objects likely to inherit properties from a prototype object besides some custom ones), but I'm not sure in that 100%.
Edit:
I asked this after reading in first edition of JavaScript: The good parts, book about prototypes - The chapter starts:
Every object is linked to a prototype object.
But it seemed a bit wired to me after what I read on objects in various places and I also read some definitions for prototype that were unclear to me; I wanted to see if I have a primal, basic of the concept of "prototype" in JS.
Nearly all objects in JavaScript are instances of Object; a typical object inherits properties (including methods) from Object.prototype, although these properties may be shadowed (a.k.a. overridden). However, an Object may be deliberately created for which this is not true (e.g. by Object.create(null)), or it may be altered so that this is no longer true (e.g. with Object.setPrototypeOf).
Javascript MDN
No, it's false.
You can create an object without prototype:
var obj = Object.create(null);
To check whether an object has a prototype, you can do
console.log(Object.getPrototypeOf(someObject));
I'm new in JavaScript. As we know Object is property on window and how can window be an instance of an Object? Are they share some method? And can I create an object like that? Sorry for my English, thank before. :)
window.hasOwnProperty("Object"); // return true
window instanceof Object // return true
The short answer is that the global Window object is an Object and can be thought of as an instance of an Object, but in JavaScript, this is all synthesized.
The tl;dr answer…
Coming to JavaScript from other OO languages such as C++ or Java can be confusing. JavaScript is not a pure OO language in the same way as the other languages. In particular, there is no "class" pure-declaration that passively acts as a template for instances and sub-classes, in the traditional sense. As an interpreted language (rather than a compiled language such as C++ and Java), JavaScript is based on "prototype" functions; class/sub-class relationships are simulated by how instances are instantiated and the "chain" of prototype references through the __proto__ attribute of each function.
Though the class keyword has been added to recent JavaScript standards, its implementation is built upon the previously existing mechanism of function definitions and prototype chains. "Sub-classing" can be achieved by setting a function's prototype attribute before calling new against the function. This will create an object-instance with its __proto__ attribute defined by the value of the prototype attribute.
When looking for member data and methods, if not in the current instance, the chain of __proto__ attributes are searched for the member until it is found or the end of the chain is reached.
I am just curious about the behavior of dynamically changing constructor's prototype in JavaScript. And I got the above result.
It seems like your already constructed instance will not share the "new" prototype's properties.
I was wondering if it's simply just the choice of such design? Because I expected to see the instance share the "new" prototype's properties. Thanks!
When an object is constructed, its prototype reference is set at that point according to the value of the constructor function's prototype property. If the constructor property later changes value, the already-constructed objects are not affected.
That's just how it works.
So, yes, that can cause problems. I guess you could exploit it for some purpose, but that'd be pretty strange.
I tried changing the constructor function of a class by
className.proptotype.constructor = newConstrcutor;
This property now refers to the newConstructor but when i try to create an instance of the class, still the old constructor is executed. So, what exactly is the use of className.proptotype.constructor ?
Thanks
Each constructor has a pointer to an object called prototype. The prototype is the main mechanism that JavaScript supports to share properties and methods among objects of the same type.
In addition, the prototype can be used to support inheritance; not exactly in the way that it is done in other OO languages like Java, c# etc. So you can imagine that there is a mechanism that allows the interpreter to navigate from one prototype to another via a chain that is implicitly formed.
In the same way that constructor point to a prototype object, the prototype can also point to its constructor; You can think of it as a relation between constructors and prototypes.
One of the uses of the className.proptotype.constructor property is when you want to debug your code. A JavaScript debugger would clearly state the type (the constructor it was created from) of an instance.
for more details you can read this
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
I was doing a little reading up on OOP in JavaScript and was learning a bit about using the prototype object. In this article (a very good read, btw), the author says the following regarding declaring methods as attributes of a JavaScript class versus declaring the method on the prototype of the class:
This is actually not the optimal way to do it. A better way is to define the method on Person.prototype. Why is this better? Anyone? Anyone? Beuller? In the first version, each time you create a person, a new sayHi function will be created for him, where as in the second version, only one sayHi function is ever created, and is shared amongst all persons that are created - because Person.prototype is their parent. Thus, declaring methods on the prototype is more memory efficient.
After reading this, I began to thing about a JavaScript framework that I occasionally use to ease OOP with the language, Classy. In the examples given in their documents, and in pretty much all Classy code that I've ever written, I always declared the methods of a class directly as an attribute of it (as in right between the curly brackets instead of outside). I realize that directly declaring a method of a Classy class is done via object notation (JSON) instead of a normal JavaScript class having methods added directly inside of the function of the class.
What I am wondering is if objects created with Classy already take advantage of the performance savings of a method declared against a class's prototype. If I declare class methods in the same way as Classy's documentation, are the methods declared per instance of the class or declared as one method for all instances of the class to share? If the former, then am I able to, or supposed to, declare methods in a Classy class's prototype?
Sorry if I sound confusing.