Where it gets complicated is when you do inheritance. You can do it this
way:
(function(window){
function Person(name, address){
this.name = name;
this.address = address;
}
Person.prototype = {
sayHello: function() {
console.log(this.name + " says hello
from"+this.address);
};
};
Person.SPECIES = "human";
function NeoPerson(name, address){
// kind of like super()
Person.call( this, name, address );
// or (you only need one)
Person.apply( this, arguments );
}
// protects the prototype chain, without invoking the parent constructor
// Object.create will need a polyfill in some browsers.
NeoPerson.prototype = Object.create( Person.prototype );
// manually sync class props
NeoPerson.SPECIES = Person.SPECIES;
// can't use object literal syntax in child classes
NeoPerson.prototype.sayHello = function() {
// call parent method
Person.prototype.sayHello.call( this );
console.log(this.name + " says \"I know Kung Fu.\" ");
}
window.Person = Person;
window.NeoPerson = NeoPerson;
})(window);
var foo = new NeoPerson( 'Neo', 'somewhere' );
foo.sayHello();
// logs both sayHellos
But you can see how verbose that it, which is why it can be useful to
use a framework like Ross's or underscore.js.
Also, there is Jangaroo, which compiles AS3 to JS. That is closer to AS3
than haXe. ;-)
Personally, I'm a big fan of TypeScript from what I've seen so far. I'll
be looking at that much more closely soon.
Kevin N.
On 10/26/12 11:30 AM, Merrill, Jason wrote:
Yeah, unfortunately the world has accepted Javascript without much complaint.
But here's a simple example of simulating a class in Javascript - put this in a
js file:
(function(window){
function Person(name, address){
this.name = name;
this.address = address;
}
Person.prototype.sayHello = function(){
console.log(this.name + " says hello from"+this.address);
};
Person.SPECIES = "human";
window.Person = Person;
}(window));
Then in HTML you can do this after importing the above js file:
var jason = new Person("Jason Merrill", "123 Smith Street");
jason.sayHello();
Jason Merrill
Instructional Technology Architect II
Bank of America Global Learning
703.302.9265 (w/h)
_______________________
-----Original Message-----
From: flashcoders-boun...@chattyfig.figleaf.com
[mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hunter
Sent: Friday, October 26, 2012 11:23 AM
To: Flash Coders List
Subject: Re: [Flashcoders] AS3
Thanks Kevin, I guess what I meant was a more object orientated approach, which
I have enjoyed learning and using in AS3. I'll look into those libraries you
mentioned.
David
On 26 October 2012 16:11, tom rhodes <tom.rho...@gmail.com> wrote:
how depressing that a simple AS3 question has turned into a thread
about how everyone is now coding JS!!
whilst we're on that subject though...
http://haxe.org/doc/targets/js
http://www.haxejs.org/
is about as close as you are going to get to AS3 in terms of JS. i
can't recommend haxe enough for a million reasons but the JS target
has just improved massively in the code it generates and haxe 3 is a
few months away which should have the complete html5 spec covered as
standard, including webgl.
On 26 October 2012 16:40, Ross Sclafani <ross.sclaf...@gmail.com> wrote:
my framework lets you code like this:
_package('com.neuromantic.display.shapes',
_import( 'com.neuromantic.display.shapes.Oval'),
_class( 'Circle' )._extends( 'Oval',{
Circle: function ( size ) {
this._super( size, size );
}
})
);
Ross P. Sclafani
design / technology / creative
http://ross.sclafani.net
http://www.twitter.com/rosssclafani
http://www.linkedin.com/in/rosssclafani
[347] 204.5714
let go of even your longest held beliefs, the only truth is in
observation.
On Oct 26, 2012, at 10:25 AM, Kevin Newman <capta...@unfocus.com> wrote:
JS doesn't have classes, and emulating them is somewhat tricky
using
the
prototype chain (it can be done though).
The easiest way to emulate classes though is to use a framework
like
underscore.js (which Backbone.js is built on).
Kevin N.
On 10/26/12 9:21 AM, David Hunter wrote:
I'd
really like to learn to approach javascript from a class-based
approach,
--
David Hunter
www.davidhunterdesign.com
+44 (0) 7869 104 906
@DHDPIC
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders