Comments (7)
I soniciq, I did that with Super.js before - unfortunately it had it's issues - you could never be sure what was available when creating modules etc. It's the same argument for having a core library with Ruby, for example, or always including models with Rails apps.
What we could start doing though, is use sprockets. Although there would be a bundled file containing everything, you could always use the individual source files if you just wanted say, class support. What do you think?
from spine.
Sprockets sounds like the way forward... I am currently using a simplified version of your Class object, you will see that I am using 'klass' and 'superclass' properties in place of your 'parent' property as I commonly find myself needing to use 'parent' when dealing with tree structures etc. Am I on the right lines with those names? My head hurts when it comes to prototypal inheritance but thanks to the simplicity of your code I think I've finally grasped it...
var Class = {
prototype: { initializer: function() {} },
subclass: function(include, extend){
var object = Object.create(this);
object.superclass = this;
object.prototype = Object.create(this.prototype);
if (include) { object.include(include); }
if (extend) { object.extend(extend); }
return object;
},
init: function(){
var initance = Object.create(this.prototype);
initance.klass = this;
initance.initializer.apply(initance, arguments);
return initance;
},
include: function(obj){
for (var key in obj) { this.prototype[key] = obj[key]; }
return this;
},
extend: function(obj){
for (var key in obj) { this[key] = obj[key]; }
return this;
}
};
from spine.
Sorry for the double comment but is there any way to adapt the Class object to allow for instantiating with the 'new' keyword? I prefer the prototypal inheritance but have a lot of code that instantiates with 'new' using John Resig's Simple Inheritance. I basically want to replace my class implementation but not have to rewrite legacy code.
from spine.
Yes, I was not sure about the naming scheme either, specially as a instances
parent is the class, and a classes parent is the class it inherits from -
probably a bit confusing. The real parent is proto. Anyways...
As to your question, unfortunately it's not possible. You can only use the
'new' keyword with constructor functions, and unfortunately functions don't
allow you to proper inheritance - static properties have to be copied. It's
for this reason that Prototype's class implementation does't inherit static
(i.e. class) properties.
On Thu, Apr 28, 2011 at 3:38 PM, soniciq <
[email protected]>wrote:
Sorry for the double comment but is there any way to adapt the Class object
to allow for instantiating with the 'new' keyword? I prefer the prototypal
inheritance but have a lot of code that instantiates with 'new' using John
Resig's Simple Inheritance. I basically want to replace my class
implementation but not have to rewrite legacy code.Reply to this email directly or view it on GitHub:
https://github.com/maccman/spine/issues/11#comment_1073433
Alex MacCaw
(415) 513-0975
@maccman
http://alexmaccaw.co.uk | http://www.leadthinking.com | http://socialmod.com
from spine.
So I suppose 'parent/klass/superclass' aren't needed as proto always returns the parent?
Thanks for the clarification on the 'new' keyword, that makes sense... I've been writing javascript for 10 years and never really understood prototypal inheritance properly. It seems 'new' just adds complexity that's not really needed.
from spine.
I've just noticed that 'instanceof' doesn't work with this approach... am I missing something?
from spine.
You may find this useful... I cant' get instanceof to work as it only works with instances created with a constructor but if you add the following to the Class.prototype, you get a kindOf method that works like Ruby's is_a/kind_of:
prototype: {
initialize: function() {},
kindOf: function(klass) {
var match = this.klass;
while (match && match !== klass) { match = match.superclass; }
return match === klass;
}
}
from spine.
Related Issues (20)
- code help( help with the completion of the code to run well) HOT 3
- testing new issue HOT 5
- Why there are two different spinejs? HOT 4
- Do not mix up instance property and local variable HOT 5
- Broken link to coffeescript in README
- Crashes if the browser doesn't support localStorage HOT 1
- Triggering refresh on model instance no longer triggers refresh on the model class HOT 9
- `isBlank` suppose to check the number? HOT 4
- Support multiple remote hosts (override Spine.Model.host) HOT 1
- 1.6 ajax create success and then auto ajax update HOT 9
- hasOne and hasMany calls inconsistent
- listeningTo the same event on different models is broken (events.split / cannot read property 'obj' of undefined / etc.) HOT 3
- Extend POST/DELETE fix from 1.6.0 to PUT calls HOT 1
- spinejs.com domain registration has expired HOT 3
- Model#changeID does not persist the new id HOT 1
- Documentation link in README.md is broken HOT 2
- Model foreign key reference with no id broken HOT 2
- Model.find unable to get correct result
- Hello. Is this project abandoned? HOT 2
- Pug support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spine.