简体   繁体   中英

Javascript class inheritance w/ both this._super and proper defineProperty descriptors

I really grok with John Resig's simple inheritance method . It has nice syntax and the this._super is super powerful.

It's 2014 tough, and I want to be able to define getters & setters along with other descriptors (but still maintain the simplicity of the Resig version if possible).

How would I got about that while keeping the syntax akin to Resig's that I hold so dear?

My dream is something like this:

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
  tools: {                    // <---- this would be so awesome
     get: function() { ... },
     set: function(v) { ... },
     enumerable: true
  },
});

var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  },
  tools: {
     get: _super,           //  <---- and this too
     set: function(v) {
        this._super(v);
        doSomethingElse();
     }
  }
});

I don't know why you'd want to do this since you could easily circumvent this by the nature of JavaScript objects, but I liked the spirit of your question.

Rather than define the method in your class, I figured why not define it for all classes? In eJohn's code I added two functions right after he declares prototype as a variable. It's a bit long for StackOverflow so please check out this cool pen I made for a more clear example.

...// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;

prototype.set = function (attr, val) {
  return this[attr] = val;
}

prototype.get = function (attr) {
  return this[attr];
}

// Copy the properties over onto the new prototype ...

And then your classes would look like this:

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});

var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  },
  set: function (attr, val) {
    this._super(attr, val);
    console.log('doing other things');
  }
});

So you can do stuff like this:

var p = new Person(true);

p.get('dancing');        // => true
p.set('dancing', false); // Telling the person to please stop dancing (he's drunk)
p.dance();               // => false... "whew!"
p.get('dancing')         // => false - he must be asleep

var n = new Ninja();

n.get('dancing');       // => false, ninjas don't dance
n.set('dancing', true); // except my ninjas do
n.get('dancing');       // => true, cause they're rad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM