Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

A better JavaScript constructor pattern. **Unmaintained**

Notifications You must be signed in to change notification settings

shannonmoeller-archive/func

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

func.js - Zero class and proud of it.

A better JavaScript constructor pattern. Abstracts away the bad parts of new, but maintains a distinction between instance and static members, unlike the illustrious Object.create.

Installation

Server-side (Node.js):

$ npm install func

Client-side (component(1)):

$ component install shannonmoeller/func.js

API

Func([...args])

...args Zero or more arguments to pass to init.

Returns an instance of Func. Calls this.init with provided arguments.

var Func = require('func');

Func.prototype.init = function (foo, bar) {
    this.yell(foo, bar);
};

Func.prototype.yell = function (baz, bat) {
    console.log(baz, bat);
};

Func('hello', 'world'); // logs 'hello world'

new Func([...args])

Same as Func([...args]). Rest in peace new.

new Func('hello', 'world'); // logs 'hello world'

Func.call(ctx, [...args])

Nice try. Same as Func([...args]).

Func.call({}, 'hello', 'world'); // logs 'hello world'

Func.apply(ctx, args)

Guess. Yep, same as Func([...args]).

Func.apply({}, ['hello', 'world']); // logs 'hello world'

Func.extend([prot], [stat])

prot An object containing instance members.

stat An object containing static members.

Returns a function whose prototype object is an instance of Func—a subclass if you must. Conveniently copies given instance and static members to the appropriate objects. The new function is also blessed with its own .extend(), you know, for kids.

var Func = require('func');

var Kid = Func.extend();

var Grandkid = Kid.extend({
    bar: 'world',
    init: function (foo) {
        console.log(foo, this.bar);
    }
}, {
    yell: function (foo, bar) {
        alert(foo + ' ' + bar);
    }
});

Grandkid('hello'); // logs 'hello world'
Grandkid.yell('hello', 'world'); // alerts 'hello world'

Shout-outs

License

MIT

About

A better JavaScript constructor pattern. **Unmaintained**

Resources

Stars

Watchers

Forks

Packages

No packages published