Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries

Category: Programming
Author: Stoyan Stefanov
4.3
All Stack Overflow 17
This Year Stack Overflow 2
This Month Stack Overflow 3

Comments

by anonymous   2019-07-21

As Pointy has pointed out, in his answer

The "constructor" property is a reference to the function that created the object's prototype, not the object itself.

The usual way to deal with this is to augment the object's prototype constructor property after assigning to the prototype

function Parent() {
    this.prop1 = "value1";
    this.prop2 = "value2";
}

function Child() {
    this.prop1 = "value1b";
    this.prop3 = "value3";
}
Child.prototype = new Parent();

// set the constructor to point to Child function
Child.prototype.constructor = Child;

var child = new Child();

// this is expected
console.log(child instanceof Child); //true
console.log(child instanceof Parent); //true

// corrected
console.log(child.constructor == Child); // now true
console.log(child.constructor == Parent); // now false

console.log(Child.prototype.constructor); // Child
console.log(Parent.prototype.constructor); // Parent

I can't recommend Stoyan Stefanov's Object Oriented JavaScript enough which covers Prototype and Inheritance in some detail (get the second edition if you can as it addresses some critiques of the first edition).

by anonymous   2019-07-21

Where can I find more info about it?

I saw this pattern in a book called Object-Oriented Javascript by Stoyan Stefanov.

http://www.amazon.co.uk/Object-Oriented-Javascript-Stoyan-Stefanov/dp/1847194141

by anonymous   2019-07-21

Like @Chales Tuang mentioned, I got the expected output on

jsfiddle

var option={
  value:'',
  desc:{
   en:'',
   hn:''
 }
};
option.value='15';
option.desc.en='hello';
option.desc.hn='world'; 

console.log(option);

option.value='25';
option.desc.hn='India';

console.log(option); 

The only explanation I could possibly think of for the case where you get same output in each call to console.log, is that the browser is applying the principle that objects are live in Javascript, as shown by Stoyan Stefanov in his book Object Oriented Javascript

As a result, the when you call console.log, it sees the latest, up-to-date property(ies) of the object, even though the object was augmented after the call, in as much as the update is within the scope of the call, it just grabs the current updated state of the object.

That's my thought on this, anyway.

by anonymous   2017-08-20

Books

Videos

  • "Advanced JavaScript" videos by Douglas Crockford. Many other interesting videos are available at Yahoo! Developer Network.

On Stack Overflow

  • A Stack Overflow discussion on JavaScript closure
  • A Stack Overflow discussion on What exactly does “closure” refer to in JavaScript?
  • A Stack Overflow discussion on Prototypal vs Functional OOP in JavaScript

Others

  • Introduction to Object-Oriented JavaScript - Modzilla
  • JavaScript Closures for Dummies
  • JavaScript Closures
  • JavaScript Closures 101
  • Private Members in JavaScript by Douglas Crockfond
  • Classical Inheritance in JavaScript by by Douglas Crockfond
  • Prototypal Inheritance in JavaScript by by Douglas Crockfond
  • JavaScript, time to grok closures
by anonymous   2017-08-20

Some good sources for Object-Oriented JavaScript and JavaScript in general...

Online Articles

  • How to "properly" create a custom object in JavaScript?
  • https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript
  • http://mckoss.com/jscript/object.htm
  • http://ejohn.org/blog/simple-javascript-inheritance/
  • JavaScript: How To Get Private, Privileged, Public And Static Members (Properties And Methods)

Books

I hope this helps.
Hristo

by Ash   2017-08-20

That is a self executing anonymous function. The () at the end is actually calling the function.

A good book (I have read) that explains some usages of these types of syntax in Javascript is Object Oriented Javascript.

by anonymous   2017-08-20

My first question is: what is this kind of object (not defined as a function) called?

That is knows as object literal notation.

My second question is: What is this kind of object (defined as a function) called?

That is knows as function expression.

More Readings:

  • Named function expressions demystified
  • Function Declarations vs. Function Expressions

If you define an object as a function, how do you define properties on the object if you don't necessarily want them to execute when it is instantiated?

You can use this keyword to add properties or use prototype property like this:

var example = function( message ) {
    alert('message');
    this.hello = function(){
        alert('hello'); 
    }
}

Or

var example = function( message ) {
    alert('message');
}

example.prototype.hello = function(){ 
   alert('hello');
}

var e = new example('hello');
e.hello();

When using this, you need to use new keyword to instantiate the object.


Interested readings on OOP:

by anonymous   2017-08-20

this is very good question)

  • in JS functions are objects, so by calling ninja.changeName("Bob") you go straight to ninja's method .changeName()

  • if ninja.changeName was called no actions outside this method will be fired

  • this.changeName ( name); - is constructor actions, they are applied to ninja object on creation only (to extend the newly created object with property name)

take a look at this great book on OOP in JS