1

I am having a theory question in my mind while learning Java Script (OOP concepts), until now i have learn two of the following way in which we can define object and initialize it's property and set its value for eg:

// 1st method 
var obj1 = {
  name: "John",
  middleName: "XYZ",
  lastName: "David",
};
document.write(obj1.name + obj1.middleName + obj1.lastName);

In the above method the object : obj1 is defined using a keyword (var) which is use for declaring a variable but here we are using it to define an object.Now i know that this object is global and i can acces its property anywhere inside a function or outside a function but what concerns me that this object has no instance to refer (i.e we cannot use instanceof keyword for this object) since i am familar with "Java" language in which every object is an instance of it's class but in JavaScript its a different concept to understand in short my question is : What exactly does the above object is referring to or is this object initialized automatically even when there is no class declared for this object ?

//2nd method

var obj2 = new Person("David");
function Person(name)
{
  document.write(name + " " + "is a person");
  
}  
 document.write(" ");
 document.write(obj2 instanceof Person);

In second method the object : obj2 is an object of a function Person() again being familiar with "Java" concept i thought that the obj2 is an object of class Person and Person() being its constructor for the class Person in short my question is : How can an object be an instanceof a function without a class being declared first ?

Mehul Chachada
  • 563
  • 9
  • 24
  • Javascript doesn't have classes. It uses prototypical inheritance, not class-based inheritance. I'm sure you've heard this before, but this is crucial to sink in. A function becomes a *constructor function* when called with `new`, at which point the resulting object is an instance of that *constructor function*, which for all intents and purposes is the same thing as being an instance of a class. Just without classes. And yes, I'm leaving a ton of asterisks and caveats out in this simplified description. – deceze Aug 23 '16 at 14:42
  • [In Javascript, functions are first class citizens](https://en.wikipedia.org/wiki/First-class_function). OOP goes way beyond classes. – Geeky Guy Aug 23 '16 at 14:43
  • Essentially you're asking how prototypical inheritance works, which is… a bit broad, and has surely been discussed to death before. – deceze Aug 23 '16 at 14:43
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Teemu Aug 23 '16 at 14:45
  • @deceze it explains the second method a much clear but what about when declared using method 1 ? whats the case then ?? – Mehul Chachada Aug 23 '16 at 14:46
  • `({}) instanceof Object` → `true`, you simply have a standard boring `Object` in that case, no more specialised "class". – deceze Aug 23 '16 at 14:53
  • And even the new `class` keyword and stuff are just syntactic sugar for the same old prototypal inheritance and don't add any other mechanics to the language. – Thomas Aug 23 '16 at 15:00

2 Answers2

1

JavaScript does not have the concept of classes built into it. There are no classes but objects and links between those objects. The objects are linked to each other using the "prototype" property. When you use new with a function name, the object created can be what you return explicitly or "this" within the function if you do not return explicitly. You can find detailed information at You-Dont-Know-JS/this & object prototypes. When not declared using the new keyword, the object you are assigning to it is automatically linked to Object.prototype. This contains several methods that you can invoke via delegation from the newly created object. You can explicitly create an object linked to another using the Object.create method passing in the object to be linked to as the first parameter. If you do not want your new object to be linked to any other object, you can simply create it as Object.create(null). There are other ways to link objects, however Object.create was introduced recently to simplify the process.

0

I found this helpful to my question and it explains a lot about what i was asking this question for, since every function in java script is treated as an object hence the object declared using new will be an instance of that function

var obj2 = new Person("David"); // Instance of Person()
function Person(name) 
{
   //Constructor declared also its an object of prototype i.e. Person.prototype

}
document.write(obj2 instanceof Person);
// true, because: Object.getPrototypeOf(obj2) === Person.prototype

which in turn is the object of prototype object and in the first method above which is

var obj1 = {
  //Rest codes go here
};

has an instance of it's constructor method which is by default Object

 var obj1 = {
  name:David,
  middleName:XYZ,
  lastName:John
}; 
// Is same as
var obj1 = new Object();
  obj1.name=David;
  obj1.middleName=XYZ;
  obj1.lastName=John;
  document.write(obj1 instanceof Object);
  // true, because: Object.getPrototypeOf(obj1) === Object

Hence in Javascript an object has its instance based on its Constructor method if define explicitly then it is the instance of that function or it is by default instance of Object method

Reference's:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

JavaScript: Class.method vs. Class.prototype.method

Every Object is a function and every function is Object - Which is Correct?

http://www.w3schools.com/js/js_object_definition.asp

Community
  • 1
  • 1
Mehul Chachada
  • 563
  • 9
  • 24