www.webdeveloper.com
Results 1 to 4 of 4

Thread: Simple inheritance

  1. #1
    Join Date
    Jul 2014
    Posts
    1

    Simple inheritance

    Hi,

    I'd like to know what you think about my implementation of inheritance. It's pretty small and intuitive I think, but i don't know if there are any drawbacks.

    Actual code:
    Code:
    Object.prototype.__extends = function __extends(parent){
        var child = this;
        if(parent){
            child.prototype = new parent();
            child.prototype.__super = function __super(){
                if(child.prototype[arguments.callee.caller.name]){                
                    return child.prototype[arguments.callee.caller.name].apply(this,arguments);
                }else{
                    return child.prototype.constructor.apply(this,arguments);
                }
            }
        }
        return child;
    }
    Usage:

    Code:
    function Parent(args) {
        // set up Parent class
        this.someFunction = function someFunction(args){
             // do something
        }
    }
    
    Child = function Child(args){
       this.__super(args);  // not needed if parent constructor has no arguments
       // set up child class
       this.someFunction = function someFunction(args){ // no anonymous function here!
           this.__super(args); // calls Parent.someFunction !
           // do something else
       }
    }.__extends(Parent);  // This is where the magic happens
    
    child = new Child(args);
    Example on JSFiddle :http://jsfiddle.net/xU86q/

    Now tell me what you think

    Julian

  2. #2
    Join Date
    Mar 2009
    Posts
    492
    For what little it is worth:

    From http://javascriptweblog.wordpress.co...cript-natives/

    Extending Object.prototype “is Verboten”¹

    For some or all of these reasons, the JavaScript community has considered Object.prototype extensions taboo for several years, and you’re very unlikely to see such extensions in production code or respected frameworks. I won’t tell you never to augment Object.prototype but I will tell you that doing so will make you a social pariah.
    From https://developer.mozilla.org/en-US/...guments/callee

    Another feature that was deprecated was arguments.callee.caller, or more specifically Function.caller. Why is this? Well, at any point in time you can find the deepest caller of any function on the stack, and as I said above looking at the call stack has one single major effect: it makes a large number of optimizations impossible, or much much more difficult. For example, if you cannot guarantee that a function f will not call an unknown function, it is not possible to inline f. Basically it means that any call site that may have been trivially inlinable accumulates a large number of guards:
    And I certainly haven't tested it, but I suspect that there will be a big performance hit with any objects you create by doing this.

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by Tcobb View Post
    For what little it is worth:

    From http://javascriptweblog.wordpress.co...cript-natives/

    Extending Object.prototype “is Verboten”¹

    For some or all of these reasons, the JavaScript community has considered Object.prototype extensions taboo for several years, and you’re very unlikely to see such extensions in production code or respected frameworks. I won’t tell you never to augment Object.prototype but I will tell you that doing so will make you a social pariah.
    I'm not sure about this one... Actually isn't that exactly what framework developers do? What I want to say is I have never read somewhere not to extend Object.prototype.

  4. #4
    Join Date
    Mar 2009
    Posts
    492
    I'm not sure about this one... Actually isn't that exactly what framework developers do? What I want to say is I have never read somewhere not to extend Object.prototype.
    The main problem is when you are using other people's libraries. Extending the generic Object function applies to all objects created. And what can happen if :

    Code:
    //In script A you have a line:
    Object.prototype.ripple = function(){ //some code };
    
    //and now in script B you have a line:
    Object.prototype.ripple = false;
    Disaster.

    And what if the other code relies upon enumerating through an object's properties?

    Code:
    Object.prototype.zap = function(){console.log("hello");};
    
    function DataPair(w, q){ 
        this.a = w;
        this.b = q;
    }
    
    var x = new DataPair(2, 3);
    
    for(i in x){
        console.log(i);
    }
    
    /*you get
    a
    b
    zap
    */
    Bad things can happen again, especially if the other code is not prepared to deal with some unknown property during the iteration.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles