mirror of
				https://github.com/flynx/object.js.git
				synced 2025-10-31 03:20:09 +00:00 
			
		
		
		
	more docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									ba185abada
								
							
						
					
					
						commit
						c9186d6d04
					
				
							
								
								
									
										14
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								README.md
									
									
									
									
									
								
							| @ -15,13 +15,14 @@ several advantages: | |||||||
| - less restrictive: | - less restrictive: | ||||||
|     - `new` is optional |     - `new` is optional | ||||||
|     - all input components are reusable |     - all input components are reusable | ||||||
|  | 	- no artificial restrictions | ||||||
| 
 | 
 | ||||||
| Disadvantages compared to the `class` syntax:   | Disadvantages compared to the `class` syntax:   | ||||||
| - no _syntactic sugar_ | - no _syntactic sugar_ | ||||||
| - slightly more complicated calling of _super_ or `parent` methods | - slightly more complicated calling of _super_ or `parent` methods | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ## Usage | ## Basic usage | ||||||
| 
 | 
 | ||||||
| ```javascript | ```javascript | ||||||
| var object = require('ig-object') | var object = require('ig-object') | ||||||
| @ -31,20 +32,18 @@ Create a basic constructor... | |||||||
| 
 | 
 | ||||||
| ```javascript | ```javascript | ||||||
| // NOTE: new is optional here... | // NOTE: new is optional here... | ||||||
| var A = new object.Constructor('A', {}) | var A = new object.Constructor('A') | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| In _JavaScript_ constructor `B` inherits from constructor `A` iff  | In _JavaScript_ constructor `B` inherits from constructor `A` iff  | ||||||
| `B.prototypes` is _prototype_ of `A.prototype`. So to implement inheritance  | `B.prototype` is _prototype_ of `A.prototype`. So to implement inheritance  | ||||||
| we simply need to _link_ the prototypes of two constructors via `.__proto__`, | we simply need to _link_ the prototypes of two constructors via `.__proto__`, | ||||||
| `Object.create(..)` or other means. | `Object.create(..)` or other means. | ||||||
| 
 | 
 | ||||||
| ```javascript | ```javascript | ||||||
| // NOTE: we could simply use A() or new A() here but that would call |  | ||||||
| //      the active constructors if they are defined which might not be |  | ||||||
| //      desirable at definition time... |  | ||||||
| var B = object.Constructor('B', {__proto__: A.prototype}) | var B = object.Constructor('B', {__proto__: A.prototype}) | ||||||
|  | 
 | ||||||
| var C = object.Constructor('C', Object.create(B.prototype)) | var C = object.Constructor('C', Object.create(B.prototype)) | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| @ -87,6 +86,7 @@ var Item = object.Constructor('Item', { | |||||||
|     __init__: function(){ |     __init__: function(){ | ||||||
|         // call the "super" method... |         // call the "super" method... | ||||||
|         object.parent(this.__init__, this).call(this) |         object.parent(this.__init__, this).call(this) | ||||||
|  | 
 | ||||||
|         this.item_attr = 'instance attribute value' |         this.item_attr = 'instance attribute value' | ||||||
|     }, |     }, | ||||||
| }) | }) | ||||||
| @ -97,7 +97,6 @@ var Item = object.Constructor('Item', { | |||||||
| ### Callable instances | ### Callable instances | ||||||
| 
 | 
 | ||||||
| ```javascript | ```javascript | ||||||
| // callable instance constructor... |  | ||||||
| var Action = object.Constructor('Action', | var Action = object.Constructor('Action', | ||||||
|     // constructor as a function... |     // constructor as a function... | ||||||
|     function(context, ...args){ |     function(context, ...args){ | ||||||
| @ -107,6 +106,7 @@ var Action = object.Constructor('Action', | |||||||
| 
 | 
 | ||||||
| var action = new Action() | var action = new Action() | ||||||
| 
 | 
 | ||||||
|  | // the instance now is a function... | ||||||
| action() | action() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user