parent and children class each with own initialize

26 views
Skip to first unread message

Patrik Gmitter

unread,
Nov 2, 2012, 3:28:27 PM11/2/12
to jsclas...@googlegroups.com
How can i create parent class with initialize so that children class could have his own initialize method too

pseudo code

class A {
    initialize: consoloe.log('init in A');
}

class A extend B {
    initialize: consoloe.log('init in B');
}

new B;

CONSOLE OUTPUT

init in A
init in B

THANKS

James Coglan

unread,
Nov 2, 2012, 5:02:14 PM11/2/12
to jsclas...@googlegroups.com
On 2 November 2012 19:28, Patrik Gmitter <pa...@patie.info> wrote:
How can i create parent class with initialize so that children class could have his own initialize method too

var Parent = new JS.Class({
  initialize: function() {
    console.log('A');
  }
});

var Child = new JS.Class(Parent, {
  initialize: function() {
    this.callSuper();
    console.log('B');
  }
});

   
Message has been deleted

Patrik Gmitter

unread,
Nov 2, 2012, 7:12:43 PM11/2/12
to jsclas...@googlegroups.com
right i missing about callSuper();
but cannot be writed this "switched" way - i wanna all in parent and i dont have to worry about something in childrens

thank you very much :)

James Coglan

unread,
Nov 2, 2012, 7:18:13 PM11/2/12
to jsclas...@googlegroups.com
On 2 November 2012 23:12, Patrik Gmitter <pa...@patie.info> wrote:
right i missing about callSuper();
but cannot be writed this "switched" way - i wanna all in parent and i dont have to worry about something in childrens

I'm not sure what you mean. What code do you want to be able to right, and what should it do? 

Patrik Gmitter

unread,
Nov 2, 2012, 7:29:37 PM11/2/12
to jsclas...@googlegroups.com
basicaly i need call function on parent init and need using init in children.

but all code like this.callSuper - if its possible move to parent not in children - for transparency and sure dont forget something.

James Coglan

unread,
Nov 2, 2012, 7:51:25 PM11/2/12
to jsclas...@googlegroups.com
On 2 November 2012 23:29, Patrik Gmitter <pa...@patie.info> wrote:
basicaly i need call function on parent init and need using init in children.

but all code like this.callSuper - if its possible move to parent not in children - for transparency and sure dont forget something.

You can't do this -- if you override initialize() in the subclass you must explicitly call callSuper() to invoke initialize() in the parent class, if that's what you want. If you don't override initialize() in the subclass, it inherits the method from the parent.

Patrik Gmitter

unread,
Nov 2, 2012, 8:07:44 PM11/2/12
to jsclas...@googlegroups.com
i dont know how but in backbone views something like this working.. within parent (view) extending some functions are called ...

https://github.com/documentcloud/backbone/blob/master/backbone.js#L1195

but its small OT

i look at it closer at morning. thank you
Reply all
Reply to author
Forward
0 new messages