Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Object.create to snack.beget (fix #22) #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Features
### Utilities

- Shallow object merge with `snack.extend`
- Prototypal inheritance with `Object.create`
- Prototypal inheritance with `snack.beget`
- A few other utilities (only those snack uses)

Documentation
Expand Down
2 changes: 1 addition & 1 deletion demos/jsonp/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ snack.ready(function (){
, delayRequest
, lastJSONP

var search = Object.create(searchInput).init(input[0])
var search = snack.beget(searchInput).init(input[0])

// add a subscription since it's a publisher
search.subscribe('keyup', fetch, results[0])
Expand Down
22 changes: 5 additions & 17 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ul id=nav>
<li><h2><a href="#core">core</a></h2>
<ul>
<li><a href="#Object.create">Object.create</a>
<li><a href="#snack.beget">snack.beget</a>
<li><a href="#snack.extend">snack.extend</a>
<li><a href="#snack.bind">snack.bind</a>
<li><a href="#snack.punch">snack.punch</a></li>
Expand Down Expand Up @@ -137,10 +137,10 @@ <h1><a href="#core">core</a></h1>



<h2 id=Object.create><a href="#Object.create">Object.create</a></h2>
<h2 id=snack.beget><a href="#snack.beget">snack.beget</a></h2>
<p>Creates a new object with a defined prototype. Helpful for prototypal inheritance and object templating.</p>
<h3>Signature</h3>
<pre class=sig><code>Object.create(obj)</code></pre>
<pre class=sig><code>snack.beget(obj)</code></pre>
<h3>Arguments</h3>
<ol>
<li><code>obj</code> (<b>object</b>) - The object to be used as the new object's prototype
Expand All @@ -158,7 +158,7 @@ <h3>Examples</h3>
dad.init('Casey')
dad.name //> 'Casey'

var son = Object.create(dad)
var son = snack.beget(dad)
son.name //> 'Casey'

son.init('Ryan')
Expand All @@ -177,18 +177,6 @@ <h3>Examples</h3>



<h3>Notes</h3>
<p>
<code>Object.create</code> is part of the <a href="http://www.ecmascript.org/docs/tc39-2009-043.pdf">ES5</a> spec.
This method is only implemented if it does not already exist in the environment. This implementation, however,
<b>does not support the second argument</b> of the native <code>Object.create</code> method.
</p>
<p>Implementation taken from <a href="http://javascript.crockford.com/prototypal.html">Douglas Crockford's original version</a></p>
<p>See also: <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create">MDC <code>Object.create</code></a></p>





<h2 id=snack.extend><a href="#snack.extend">snack.extend</a></h2>
<p>Performs a shallow merge of two or more objects into the first.
Expand Down Expand Up @@ -371,7 +359,7 @@ <h3>Notes</h3>


<h2 id=snack.create><a href="#snack.create">snack.create</a></h2>
<p>Similar to <a href=#Object.create>Object.create</a>, except it allows for an extension object to merge into the new object. When methods are redefined in the extension object, they are automatically <a href="#snack.punch">punched</a> providing a reference to the old (or parent) method. Useful for object templating and prototypal inheritance.</p>
<p>Similar to <a href=#snack.beget>snack.beget</a>, except it allows for an extension object to merge into the new object. When methods are redefined in the extension object, they are automatically <a href="#snack.punch">punched</a> providing a reference to the old (or parent) method. Useful for object templating and prototypal inheritance.</p>

<h3>Signature</h3>
<pre class=sig><code>snack.create(proto [, ext])</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion docs/js/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function init (){
, terms = $('#terms')
, items = $('#nav > li li')
, headers = $('#nav h2')
, searcher = Object.create(searchInput).init(terms[0])
, searcher = snack.beget(searchInput).init(terms[0])

searcher.subscribe('keyup', search)
terms.attach('click', searcher.listener.fire)
Expand Down
17 changes: 7 additions & 10 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
* Zepto.js (c) Thomas Fuchs MIT License
*/

if (typeof Object.create != 'function'){
// ES5 Obeject.create
Object.create = function (o){
function F() {}
F.prototype = o
return new F
}
}

!function(window){
var snack = window.snack = {}
, guid = 0
Expand Down Expand Up @@ -60,9 +51,15 @@ if (typeof Object.create != 'function'){
return fn.apply(obj, args)
}
},

beget: function (o){
function F() {}
F.prototype = o
return new F
},

create: function (proto, ext){
var obj = Object.create(proto)
var obj = snack.beget(proto)
if (!ext)
return obj

Expand Down
2 changes: 1 addition & 1 deletion src/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if (!nodes.length)
nodes = [nodes]

var wrapper = Object.create(proto)
var wrapper = snack.beget(proto)
, i = 0
, l = nodes.length

Expand Down