Jump to content

Isa(aggregation) and Hasa(composition) relationships


Recommended Posts


do we have Isa(aggregation) and Hasa(composition) relationships in JS?though instanceof check passes for all the types of inheritance;they are just changing the references of prototype and shadowthe instance variables, I think instanceof is implemented to return true with references only, instead of really creating the required Object.This is still fine. But, do we have Hasa relationship ?


for Hasa we can do

var o = {};function f(){var hasa=o;}but samething is also done for prototype inheritance, then what we have is only Hasa and instanceof is implemented to return true right ?

Link to comment
Share on other sites

There is no built-in inheritance in Javascript, but because Javascript is weakly typed it's not that important because you're capable of having arrays with more than one type of data in them and function parameters don't have any type assigned to them.


As for composition, you're allowed to assign a different object to an object's property.

function MyObject1() {    this.obj = new MyObject2();}function MyObject2() {}

There's a good description of the instanceof operator on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

In general, instanceof only returns true if you give it the function that was used to create the object with the new operator.

Link to comment
Share on other sites

No, not natively. Some people built a library called Backbone that was meant to emulate object-oriented programming in Javascript, but I feel like it's an unnecessary layer of abstraction.


I understand people like to continue programming in a way they're familiar with, but if you want to use Javascript most efficiently it's best to learn Javascript's own programming style.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...