Live data from Hacker News

Introduction to Object-Oriented JavaScript - MDC Docs

developer.mozilla.org

11–15 of 15 posts

Re: Introduction to Object-Oriented JavaScript - MDC Docs

#11
post #9

Under the section "The Property (object attribute)" the code segment has two curious lines. I expect the first cancels out the latter, making the latter useless. // in Person function this.gender = gender; // outer scope (gender will never be this value because it is overridden by the person constructor argument) Person.prototype.gender = 'Person Gender'; Am I correct, or does the prototype.gender line have some othe…

It's ensuring a default value basically. If you instantiate a person without an argument and try to reference this.gender later, you'll throw a script error. This is just avoiding that.

A way I'd prefer might look like this:

  function Person = function(gender) {
      this.gender = gender || 'neuter' //the sauce that defines a fallback
  }

Re: Introduction to Object-Oriented JavaScript - MDC Docs

#13
post #9

Under the section "The Property (object attribute)" the code segment has two curious lines. I expect the first cancels out the latter, making the latter useless. // in Person function this.gender = gender; // outer scope (gender will never be this value because it is overridden by the person constructor argument) Person.prototype.gender = 'Person Gender'; Am I correct, or does the prototype.gender line have some othe…

It's ensuring a default value basically. If you instantiate a person without an argument and try to reference this.gender later, you'll throw a script error. This is just avoiding that. A way I'd prefer might look like this: function Person = function(gender) { this.gender = gender || 'neuter' //the sauce that defines a fallback }

This is not the case, because in the constructor person is being set to the argument. If the argument is not supplied (undefined), calling the instantiated object's gender property will result in undefined. [tested and confirmed]

Re: Introduction to Object-Oriented JavaScript - MDC Docs

#14
post #12
post #2

> Inheritance A Class can inherit characteristics from another Class. Somebody should tell Mozilla, that there are no classes in JavaScript....

Please don't make us start saying "object used as a class".

The first step to learning JavaScript is to forget about class-ical OOP, you can't write good code if you start forcing class-ical concepts on JavaScript, it's a prototypical language encouraging people to treat it as something else is wrong.

Re: Introduction to Object-Oriented JavaScript - MDC Docs

#15
post #14
post #12

Earlier quoted context omitted.

Please don't make us start saying "object used as a class".

The first step to learning JavaScript is to forget about class-ical OOP, you can't write good code if you start forcing class-ical concepts on JavaScript, it's a prototypical language encouraging people to treat it as something else is wrong.

When a language has a deficiency people make up for it with libraries. If you don't acknowledge the "class pattern" for object inheritance then you are dismissing an incredibly popular use of OO.

Whether or not code reuse via OO is a good thing or not is a whole other debate though. I don't try to warp every problem into a class hierarchy, but sometimes it's a useful conceptual tool.

Post reply on HN