Object is property of window (global), window is instance of Object? - javascript

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.

Related

Is it true to say that all objects (besides some custom objects), has a prototype object, in JavaScript?

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

In JavaScript if "Function" descends from "Object", then how was keyword function made available while defining the constructor of object?

I was recently reading some tutorials of JavaScript, and every article states that Functions descend from Objects.
The above statements leads to an understanding that Objects were created before Functions were available.
If it is so, then how can constructor of an Object be a function (As per my understanding object Function hasn't been created yet)?
Please help me out with this confusion.
Functions Descend from Objects
In JavaScript functions are basically objects.
They are what is called first class objects - meaning they can do anything that an object can and are basically interchangeable.
Therefore all properties and methods available on the object level are also available on the function level.
You might want to read this
How can constructor of an Object be a function
The short answer is that you are trying to understand something that is irrelevant to understand in order for you to use the Javascript language for coding.
The longer answer is that you must distinguish between built-in objects:
Object
Function
Array
and the objects you create yourself as part of your code.
The built-in objects are part of the Javascript language and are simply available to you out-of-the-box. Your question relates to the internal workings of the language as you are inquiring about how the built-in Object is constructed. This has little value to understand unless you are looking to become part of the team developing the Javascript language.
The objects you create yourself as part of your code is almost always linked to other objects via the objects internal [[Prototype]] property. So if you for example do:
var x = {"foo": "bar"};
you have now created your own object with the property foo and the value bar. You can then do:
console.log(x.hasOwnProperty("foo")); //true
even though x does not have a method called .hasOwnProperty() directly on it. The reason the above code works is, that x is linked to the built-in Object.prototype via x's internal [[Prototype]] property and via this link delegates to Object.prototype.hasOwnProperty(). So all the objects you create as part of your code has access to use the methods on Object.prototype.

Can you convert a superclass

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.

Javascript - Inheritence and reference

In javascript, other than prototypal inheritence using prototypes, is there any other way of inheritence. Is the use of object literal only for creating a singleton class.
What is a reference when it comes in javascript. What are the uses of closure concept? Is there an alias in C++?
is there any other way of inheritence than prototypal inheritance.
Yes, many other patterns are available, all of them based on creating instance-specific properties on the objects. Look for parasitic inheritance or the mixin pattern.
Is the use of object literal only for creating a singleton class.
Object literals don't create classes, they create objects. Since almost everything is an object in JavaScript, an object can represent almost every kind of data structure. Most of these uses can include the creation via an object literal.
what is a reference when it comes in javascript.
Every object basically is a reference to its properties. Whenever you deal with objects, all you are holding is a reference to the actual data structure that contains the properties. You might want to get familiar with OOP. Also have a look at How to explain object references in ECMAScript terms?.
what are the uses of closure concept
Encapsulation, mostly. You won't find a exhaustive list, though, closures are pretty ubiquitous in JavaScript code.
is there an alias in C++?
Yes, C++ has closures as well, but you need to explicitly declare them.

Why className.proptotype.constructor = newConstructor() does not change the constructor of Javascript Class?

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

Categories