Git Product home page Git Product logo

javascript-spessore's People

Contributors

raganwald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

javascript-spessore's Issues

a wrapper that plays well with "new"

Probably belongs in the discussion around metaobjects


The problem is that constructors don't play nicely with combinators/wrappers. Consider:

function Widget(radius, mass) {
  this.radius = radius;
  this.mass = mass;
}
Widget.prototype.area = function() {
  return Math.PI * this.radius * this.radius;
};

w = new Widget(2, 10)
w.area()
  //=> 12.566370614359172

Great. Now let's wrap Widget:

var before = function (before, fn) {
  return function wrapped () {
    before.apply(this, arguments);
    return fn.apply(this, arguments);
  };
};

function hello () { console.log('hello'); }

FriendlyWidget = before(hello, Widget);

friendly = new FriendlyWidget(2, 10)
  //=>
    hello
    { radius: 2, mass: 10 }

Looks good so far, printed "hello" to the console and correctly initialized the properties. Let's call its area method:

friendly.area()
  //=> TypeError: Object #<wrapped> has no method 'area'

D'oh! The trouble is, the new keyword has used our wrapper function to set the prototype instead of using Widget as a prototype. You can see that in the error message, or we can test it directly:

Object.getPrototypeOf(w)
//=> { area: [Function] }

Object.getPrototypeOf(friendly)
//=> {}

Once you get into AOP-style programming, it's very common to want to give before- or after- advice to constructors. Combinators are obviously not going to work "out of the box" because of the prototype, so you have to do something else.

I'm not going to say that factory methods are the only way, or the best way. One alternative is to copy Ruby and always use a method called initialize, and wrap that instead of wrapping the constructor. Another is to do some backflips in your wrapping code to detect when the wrapper is being called as a constructor.

There are some choices to be made, but there is clearly a problem that needs to be solved if you want to use wrappers on the code that initializes an object.

Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
  obj.__proto__ = proto;
  return obj; 
}

var before = function (before, fn) {
  function wrapped () {
    if (Object.getPrototypeOf(this) === wrapped.prototype) {
      Object.setPrototypeOf(this, fn.prototype);
    }
    before.apply(this, arguments);
    return fn.apply(this, arguments);
  };
  return wrapped;
};

FriendlyWidget = before(hello, Widget);
friendly = new FriendlyWidget(2, 10)
friendly.area()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.