Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30210
| From | Scott Sauyet <scott@sauyet.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Comparing some of the ways to create objects |
| Date | 2016-04-04 04:15 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <ndspod$lhh$1@dont-email.me> (permalink) |
| References | <hbc2gbpvk5nk24b0qupps6cjqn8fm3ar5e@4ax.com> |
John Harris wrote:
> It's time to compare ways of creating objects now that the full release
> version of Firefox implements class declarations. The code below shows
> three of the ways. They are all ones where an object is created by using
> new and a constructor.
I don't see why that is so special. I will add several more suggestions
below that use three different object creation APIs, including a 'new-and-
constructor' version. Two of them also share the same sort of prototype-
based object mechanism shared by your three samples.
> These ways are more suited to cases where several
> objects of the same kind are to be created, or where the code might be
> reused in other projects. Several other ways exist but they are more
> suited to the ad hoc creation of only one object.
I believe all three techniques I suggest below are similarly suited to
the creation of several objects of the same kind. The second one
(`thing5`) is more memory-intensive, and probably is not suitable for
hundreds of thousands or millions of instances of the type, but other
than that, they have similar characteristics. `thing5` does offer the
ability to have truly private implementation details, which many other
techniques really don't.
> The three examples illustrate :
> 1 Class-style creation : Using an ES6 class declaration;
> 2 Class-style creation : Using ES3 features;
> 3 Prototypal-style creation : Using ES3 features.
These three also illustrate standard prototypal creation, regardless of
whether some syntactic sugar has been added to help make them seem more
similar to C++-style OOP classes.
> [ ... code elided ... ]
My techniques follow. I'm wondering if you see these ones as somehow
lesser techniques than the three you present. And if so, why is that?
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function Thing4(phrase) {
var count = 0;
this.toString = function() {return phrase;}
this.incr = function() {count++;}
Object.defineProperties(this, {text: {
get: function() {return phrase;}
}, count: {
get: function() {return count;}
}})
} // Thing4
var a4 = new Thing4("Hello from Thing4 a4");
a4 + ': ' + a4.count; //=> "Hello from Thing4 a4: 0"
a4.incr();
a4 + ': ' + a4.count; //=> "Hello from Thing4 a4: 1"
var b4 = new Thing4("Hello from Thing4 b4");
b4 + ': ' + b4.count; //=> "Hello from Thing4 b4: 0"
function thing5(phrase) {
var count = 0;
return {
toString: function() {return phrase;},
incr: function() {count++},
get count() {return count;}
};
} // thing5
var a5 = thing5("Hello from thing5 a5");
a5 + ': ' + a5.count; //=> "Hello from thing5 a5: 0"
a5.incr();
a5 + ': ' + a5.count; //=> "Hello from thing5 a5: 1"
var b5 = thing5("Hello from thing5 b5");
b5 + ': ' + b5.count; //=> "Hello from thing5 b5: 0"
var Thing6 = {
init: function(phrase) {
this.text = phrase;
this.count = 0;
},
incr: function() {
this.count++;
},
toString: function() {
return this.text ;
}
}; // Thing6
var a6 = Object.create(Thing6);
a6.init("Hello from Thing6 a6");
a6 + ': ' + a6.count; //=> "Hello from Thing6 a6: 0"
a6.incr();
a6 + ': ' + a6.count; //=> "Hello from Thing6 a6: 1"
var b6 = Object.create(Thing6);
b6.init("Hello from Thing6 b6");
b6 + ': ' + b6.count; //=> "Hello from Thing6 b6: 0"
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Cheers,
-- Scott
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-03 16:09 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-04 04:15 +0000
Re: Comparing some of the ways to create objects Stefan Weiss <krewecherl@gmail.com> - 2016-04-04 15:20 +0200
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-05 04:04 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-04 16:48 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-05 04:04 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-07 15:07 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-08 01:10 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-08 14:55 +0100
Re: Comparing some of the ways to create objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-04 22:12 +0200
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-05 09:57 +0100
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-06 16:41 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-06 23:42 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-07 16:09 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-08 02:05 +0000
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-08 15:43 +0100
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-09 00:01 +0000
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-09 16:46 +0000
Re: Comparing some of the ways to create objects Scott Sauyet <scott@sauyet.com> - 2016-04-10 16:51 +0000
Re: Comparing some of the ways to create objects Stanimir Stamenkov <s7an10@netscape.net> - 2016-05-08 17:27 +0300
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-10 11:31 +0100
Re: Comparing some of the ways to create objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-10 11:52 +0100
csiph-web