Nathan-Wall / proto

A programming language derived from JavaScript which emphasizes prototypes, integrity, and syntax.
Other
12 stars 1 forks source link

Come up with an easy way to convert JS constructors into Proto prototypes #69

Open Nathan-Wall opened 10 years ago

Nathan-Wall commented 10 years ago

Problem

// Where @env refers to Node environment
import { Buffer } from '@env';
var b = new Buffer();
// b now inherits from the Buffer constructor, not Buffer.prototype
// In addition, the Buffer constructor was not called

A possible way to fix this would be to have proxied JS functions behave differently when used in a new expression, though this doesn't feel like the best approach.

Another idea would be to have a function which can be called to convert a JS constructor into a Proto prototype.

import { Buffer } from '@env';
Buffer = Object.fromJsConstructor(Buffer);
var b = new Buffer();

With a definition something like:

Object.fromJsConstructor = fn(constructor) :{
    var proto = like constructor.proto;
    proto.init = constructor;
    return proto;
}

Perhaps there are some other ways to handle this situation... In general, the rules regarding passing of JS objects into Proto and Proto objects into JS need to be more refined and better thought out.

Nathan-Wall commented 10 years ago

See also #4.