Skip to content

Commit

Permalink
Merge pull request #15 from canjs/11-fix-doc-signatures
Browse files Browse the repository at this point in the history
Added signature for Construct.extend( ... ) in docs
  • Loading branch information
justinbmeyer committed Oct 17, 2016
2 parents a0be6f1 + cbc335a commit 9f40f50
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
69 changes: 55 additions & 14 deletions docs/construct.md
Expand Up @@ -9,33 +9,74 @@ without worrying about hooking up all the particulars yourself. Use
[can-construct.extend can-construct.extend] to create an inheritable
constructor function of your own.

@signature `new Construct( ...args )`

Creates a new instance using Construct's constructor functions.

@param {*} [args] The arguments passed to the constructor.
@return {Object} The instantiated object.

@body

## Use

In the example below, `Animal` is a constructor function returned by [can-construct.extend can-construct.extend]. All instances of `Animal` will have a `speak`
method, and the `Animal` constructor has a `legs` property.

```js
var Construct = require("can-construct");
var Animal = Construct.extend({
legs: 4
},
{
speak: function() {
console.log(this.sound);
}
});
```

An optional [can-construct::setup setup] function can be specified to handle the instantiation of the constructor function.
```js
var Animal = Construct.extend({
legs: 4,
setup: function(sound) {
return [sound]
}
},
{
speak: function() {
console.log(this.sound);
}
});
```
[can-construct::setup setup] returns {Array|undefined} If an array is returned, the array's items are passed as arguments to [can-construct::init init].

var Construct = require("can-construct");
var Animal = Construct.extend({
legs: 4
}, {
init: function(sound) {
this.sound = sound;
},
speak: function() {
console.log(this.sound);
}
});
In addition [can-construct::init init] can be specified which is a method that gets called with each new instance.
```js
var Animal = Construct.extend({
legs: 4,
init: function(sound) {
this.sound = sound;
}
},
{
speak: function() {
console.log(this.sound);
}
});
```

For more information on deciding when to use [can-construct::setup setup] or [can-construct::init init]
see the bottom of the [can-construct::setup setup] documentation.

You can make instances of your object by calling your constructor function with the `new` keyword. When an object is created, the [can-construct::init init]
method gets called (if you supplied one):

var panther = new Animal('growl');
panther.speak(); // "growl"
panther instanceof Animal; // true
```js
var panther = new Animal('growl');
panther.speak(); // "growl"
panther instanceof Animal; // true
```

## Plugins

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -42,7 +42,7 @@ animal.sayHi();

Construct.extend("ConstructorName",{})

returns a constructur function that will show up as `ConstructorName`
returns a constructor function that will show up as `ConstructorName`
in the developer tools.
It also sets "ConstructorName" as [shortName](#shortname-string).

Expand Down

0 comments on commit 9f40f50

Please sign in to comment.