6 Replies - 2136 Views - Last Post: 27 May 2012 - 12:41 AM

#1 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 11:42 AM

At least I believe I'm using Object Literal Notation (I'm very new to attempting OO Javascript).

Anyway, here is an example of my "Classes" if you will:
var registrationInitalDataObject = (function () { 
	var selectedChoice;
	
	function getSelectedChoice(){return selectedChoice;}
	function setSelectedChoice(choice){selectedChoice = choice;}
	
	// publics
	return {
		 getSelectedChoice : getSelectedChoice,
		 setSelectedChoice : setSelectedChoice
	};
}());



So everything in the return block is public. Thus, the var is private.

I would use this by doing something like this:
registrationInitalDataObject.getSelectedChoice();



That's all well and good. I like that, and I want to keep everything consistently like that throughout my Javascript.

NOW.. my question is how can I extend registrationInitalDataObject and possibly override some methods?

Like for example how could I make a "Class" called registrationInitialDataObjectPlusMessage and override the getSelectedChoice function to do something like:

function getSelectedChoice(){return selectedChoice + "Hello there!";}


I know I possibly need to do something with prototypes and stuff, but I just can't figure out how to get it to work. It's really confusing stuff.

Thanks for the help!

This post has been edited by eZACKe: 26 May 2012 - 11:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I get Inheritance with my Object Literal Notation

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 12:16 PM

Hi

First thing, what you have there is not a class, rather just an object. So technically you can't extend it, but clone it.

To apply the concept of inheritance, you first need to have classes instead of objects.

This read might help you.

This post has been edited by chinchang: 26 May 2012 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 12:39 PM

Well what I have there is the Singleton pattern:
http://www.addyosman...tternjavascript

^See Singleton Pattern in that.

I'd like my objects to be Singletons in what I'm doing. Is this possible?
Was This Post Helpful? 0
  • +
  • -

#4 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 01:01 PM

I guess then I'm really asking is there anyway to extend a Singleton?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 04:56 PM

the question is, to what extend do you want to extend the singleton. you can use this singleton object in a prototype chain without problems, but you will never have direct access to the selectedChoice variable (which is not a problem, since you have getters and setters).

var extObj = Object.create(registrationInitalDataObject);
extObj.doSomething = function () { /* code */ };
// you can also pass object properties in the Object.create() method 
// according to Object.defineProperties()

Was This Post Helpful? 1
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: How can I get Inheritance with my Object Literal Notation

Posted 26 May 2012 - 10:38 PM

Ok that makes sense to me, thanks a lot. I understand what is going on now.

Question though: What about can one Singleton extend another? Because if we're making a Singleton wouldn't it not be able to use Object.create()?
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: How can I get Inheritance with my Object Literal Notation

Posted 27 May 2012 - 12:41 AM

View PosteZACKe, on 27 May 2012 - 07:38 AM, said:

Question though: What about can one Singleton extend another? Because if we're making a Singleton wouldn't it not be able to use Object.create()?

depends on how narrow you define a Singleton and what you define as "extend". and yes, a Singleton is able to use Object.create().

on the other hand side, there is not really a point in extending a Singleton, as there is only one instance thereof. it would work equally, if you used Dependency Injection or a Decorator Pattern.
var registrationInitalDataObject = (function () { 
	var selectedChoice;
	
	function getSelectedChoice(){return selectedChoice;}
	function setSelectedChoice(choice){selectedChoice = choice;}
	
	// publics
	return {
		 getSelectedChoice : getSelectedChoice,
		 setSelectedChoice : setSelectedChoice
	};
})();

// probably the most simple method to "extend" a Singleton:
registrationInitalDataObject.newProperty = true;
registrationInitalDataObject.newMethod = function (x) {
    if (typeof x === "number") {
        registrationInitalDataObject.setSelectedChoice(x);
    }
};

This post has been edited by Dormilich: 27 May 2012 - 12:45 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1