raganwald / javascript-spessore Goto Github PK
View Code? Open in Web Editor NEWManuscript for JavaScript Spessore
Home Page: https://leanpub.com/javascript-spessore
Manuscript for JavaScript Spessore
Home Page: https://leanpub.com/javascript-spessore
Ideas:
After including the appendix, drop the imports.
Also, add unvariadic where required. Should there be a wrapUnvariadically
? What kind of combinator is Xab.(Xa)b?
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()
Also, hint that methods may also have a relationship with the prototype chain.
At the end of that document, there is an unrendered (unrenderable?) symbol K>
.
"no interleaving."
Consider dropping pattern-matching entirely as being unweildy?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.