inherit this

26 views
Skip to first unread message

houpdelta

unread,
Nov 8, 2012, 12:44:08 PM11/8/12
to prototype-s...@googlegroups.com
Hi,

I try this configuration,

// define a module
var Vulnerable = {
  wound: function(hp) {
    this.health -= hp;
alert(this.health);
  if (this.health < 0) this.kill(); }, kill: function() { this.dead = true; } };

var Display = {
clear: function() {
....
}
};


 // the first argument isn't a class object, so there is no inheritance ... // simply mix in all the arguments as methods: var Person = Class.create(Vulnerable, Display { initialize: function() { this.health = 100; this.dead = false; } });

But this.healt is undefined.

Is it possible to do that ?


Thank

Mich C

unread,
Nov 9, 2012, 12:44:31 AM11/9/12
to prototype-s...@googlegroups.com
Hi.

You code works.

var a = new Person();
a.wound(10); // 90

Only  in row

var Person = Class.create( Vulnerable, Display {

need a comma

var Person = Class.create( Vulnerable, Display, {

Also try `addMethods`

Person.addMethods(Display);
Person.addMethods(Vulnerable);

Laurent Barre

unread,
Nov 9, 2012, 1:45:08 PM11/9/12
to prototype-s...@googlegroups.com
Hi,

I think that I wrote a bad demonstration of my query.
I try again. The new code.



// define a module
var Vulnerable = {
  wound: function() {
    alert(this.health);

  }
};

 // the first argument isn't a class object, so there is no inheritance ...
// simply mix in all the arguments as methods:
var Person = Class.create(Vulnerable,{

  initialize: function() {
    this.health = 100;
  },
  test : function() {
    Vulnerable.wound();
  }
});


var a =  new Person();
a.test();

//undifined

Is that, I like to do.

Thank.



2012/11/9 Mich C <fant...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

fntzr

unread,
Nov 9, 2012, 1:58:12 PM11/9/12
to prototype-s...@googlegroups.com
You must call this.wound()

test : function() {
this.wound();
}

if yoy call Vulnerable.wound() then `this` => Object[Vulnerable].
When you create a Person then all methods from Vulnerable copy into
Person Object.

Laurent Barre

unread,
Nov 9, 2012, 2:39:21 PM11/9/12
to prototype-s...@googlegroups.com
Thank you, for your example and your explanation


2012/11/9 fntzr <fant...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to prototype-scriptaculous+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages