0

I know this type of question has been asked and answered a million times, but I still don't understand this.

Everything in JavaScript is an object. There exists a prototype chain that the JavaScript runtime searches for methods and properties. These two things are clear. I also (think I) understand what the new statement does. Maybe the problem is that I don't understand what Object.create does. I have been using the following JavaScript design pattern:

SuperClass = function(){
    this.superprop = 'some super property';
};

SubClass = function(){
    this.subprop = 'some (not so) super property';
};

SuperClass.prototype.someSuperMethod = function(){console.log("I'm super.")};

SubClass.prototype = Object.create(SuperClass.prototype);
SubClass.prototype.constructor = SubClass;

var instance = new SubClass();

instance.someSuperMethod(); // Great! That's super!

But, I have know clue why I can't write:

  1. SubClass = Object.create(SuperClass);
  2. SubClass = SuperClass;
  3. SubClass = new SuperClass;
  4. SubClass.prototype = SuperClass.prototype;
  5. SubClass = new SuperClass();
  6. SubClass.prototype = SuperClass;

(or any of the combinations above). In short, I guess I don't know what the .prototype property of a function is or why I need to Object.create this prototype if I want SubClass to inherit everything from SupClass.

I also don't understand what SuperClass.prototype.constructor means. How is this object different than SuperClass?

I also don't understand why I need to write new SubClass() and not new Subclass. What's the difference?

Community
  • 1
  • 1
fuzzybear3965
  • 243
  • 5
  • 15
  • Uh, yeah, many questions, all of them already asked. Shall I search for the duplicates? – Bergi Jul 21 '15 at 21:12
  • 1
    Why these combinations are wrong: http://stackoverflow.com/questions/17392857/benefits-of-using-object-create-for-inheritance – Bergi Jul 21 '15 at 21:13
  • 1
    What `Object.create` does: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – Bergi Jul 21 '15 at 21:14
  • 1
    What `.prototype` is: https://stackoverflow.com/questions/572897/how-does-javascript-prototype-work (it plays part in what `new` does) – Bergi Jul 21 '15 at 21:15
  • 1
    Why you can write both `new SubClass()` and `new SubClass`: https://stackoverflow.com/questions/3034941/new-myobject-vs-new-myobject – Bergi Jul 21 '15 at 21:16
  • How `SuperClass.prototype.constructor` is different from `SuperClass`? Not at all, they're (expected to be) the same object - the `.constructor` property of the prototype object points back to the constructor function – Bergi Jul 21 '15 at 21:19
  • Did I miss anything? – Bergi Jul 21 '15 at 21:23
  • @Bergi Well done! I saw the question yesterday and thought it's interesting and I would like to answer it, but it would take an essay for that. I would recommend few good books on JavaScript I have already read: JavaScript: The Good Parts by Douglas Crockford; You Don't Know JS by Kyle Simpson; Principles of Object-Oriented Programming in JavaScript by Nicholas Zakas – cezar Jul 22 '15 at 08:13
  • Alright, @Bergi. I read your comments almost immediately after they were posted. I had read those links before (and many others) but I still wanted to read another perspective. Sometimes, forming your own question and getting your own response is the best way to get an answer. But, you're totally right in that I should be able to construct these answers for myself. So, below, I'm going to try to answer my own question in however many words over however much time it takes me. I'm committed to understanding prototypical inheritance in JavaScript. – fuzzybear3965 Jul 22 '15 at 14:57
  • @Cezar, I have only heard of "JavaScript: The Good Parts". I'll be sure to, at least, check that one out. Understand exactly what's going on in the background with JavaScript is the best way to debug problems. Without that, I'll be totally lost when I'm out of scope or when an object doesn't have a method that I think that it should. – fuzzybear3965 Jul 22 '15 at 14:58

1 Answers1

0

Alright, I'm the OP for this question. But, I'm going to try to form a comprehensive answer to my question (as comprehensive an answer as satisfies me).

First things first: Most everything in JavaScript is an object. What is an object? An object is any thing with methods and properties associated with it. You might ask: "Okay, so is a string an object? Is the number 5 an object?". That's an interesting question. Strictly, no. These are so fundamental to JavaScript as a language that they are called primitives. There are only 6 primitives in JavaScript: strings, numbers, boolean, null, undefined and symbol (symbols are new as of ECMAScript 6, the latest standard on which JavaScript is based - published officially: June 17, 2015).

The main components of objects are primitives. You can have functions return strings or add numbers. A generic object could be a container for strings or numbers:

var myGenericObject = {var1: "string 1",
                       var2: 42, 
                       var3: function(){
                                 console.log("do something.")
                             }
                      };

All objects in JavaScript are children of the Object class, even functions. Don't be confused: while the Function class is a fundamental object, functions still descend from the Object class, which I guess makes the Object class the most fundamental object in JavaScript (this is probably why it's called Object). However, the Object class really descends from the null primitive (don't worry; that's where it ends). The concept that I'm sideways-addressing right now is called prototypical inheritance and it is a core distinguishing feature of JavaScript.

But, JavaScript is pretty smart. when you try to access methods that would exist for most objects Thus, var testString = "hello" with is an object

First, the new operator according to ECMAScript 5

(more in a bit. I'm just saving my progress, temporarily).

Community
  • 1
  • 1
fuzzybear3965
  • 243
  • 5
  • 15
  • "*The main components of objects are primitives*" - no, objects can hold *any* values (primitives and objects). – Bergi Jul 23 '15 at 18:10