new Person() ES3 style:
create object, set prototype, run user code
Person() ES3 style with !instanceof chunk:
run user code, check for instanceof (walk the proto chain), create 
object, set prototype, run user code
((To be technical; new Person() ES3 style with the !instanceof chunk)):
create object, set prototype, run user code, check for instanceof (walk 
the proto chain)

Person() ES5 style:
run user code, create object, set prototype
new Person() ES5 style (object created by new being discarded):
create object, set prototype, run user code, create object, set prototype

take your pick, discard an object when adding a new that doesn't belong, 
or run a function and walk the proto chain twice each when forgetting to 
use new.
Other than using fake ES5 in environments where ES5 is not implemented I 
don't see how the new ES5 stuff is any less efficient than the ES3 methods.
It's not like objects being created then quickly discarded is anything 
new in JS.
"foo".bar = "baz"; // creates a object to wrap "foo" so it can set .bar 
on it, then discards that object.
Or rather in every day use:
"foo".substr(1,1).toUpperCase(); // creates an object to wrap "foo", 
runs .substr, discards the object, wraps the new string literal in 
another object, runs .toUpperCase(), discards the object.
Well, that's basically how it's supposed to behave, I'm not sure if any 
of the interpreters optimized this case enough to avoid discarding 
anything while mimicking the defined behavior.


And that's not the only thing ES5 adds, don't forget about strict mode. 
Strict mode goes and improves programming using the old ES3 
classfunction based approach.
<script>
"strict mode";
function Person() {
    this.first = '';
    this.last = '';
}

new Person(); // Works, no instanceof overhead cruft
Person(); // Throws an error right away because undefined can't have 
properties.
</script>

In ES5 strict mode `this` inside functions is undefined instead of the 
global object.
That means that instead of the this.first = ''; meaning window.first = 
''; when you call Person() instead of `new Person()` it means 
undefined.first = '';
Thus rather than setting a global property which depending on your code 
may not throw for awhile and leak into the global object, it'll throw 
right away because it can't access/set properties on undefined.

~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]

Andrea Giammarchi wrote:
>
> Dude, I know what you mean and I use Objeect.create already via 
> vice-versa and no flags configuration but imho to better understand 
> benefits we should consider better examples. I forgot in this one 
> there is another thing to: call the init method over created instance. 
> So, again, our good old ES3 is still better than future alchemies for 
> that purpose, imho.
>
>> On Aug 14, 2009 10:08 AM, "Pauan" <pcxunlimi...@gmail.com 
>> <mailto:pcxunlimi...@gmail.com>> wrote:
>>
>>
>> The example chosen was poor. It is true that the example could be
>> trivially implemented right now.
>>
>> Object.create is most useful when either A) using inheritance without
>> functions, or B) when changing the enumerable, writable, and
>> configurable properties. In that case the extra code would be needed.
>> The example didn't demonstrate either of those cases, so of course it
>> looks like meaningless boilerplate.
>>
>> As was said, "You could even chose to make those non-enumerable or
>> lock them with values." That was the point, I believe. It allows you
>> not only to remove the "new" at the front of constructors, but also
>> allows you to configure property flags.
>>
>> I already provided an example that shows how to do inheritance without
>> needing constructor functions at all, which I believe was the primary
>> intent of Object.create.
>>
>> On Aug 14, 1:47 am, Andrea Giammarchi <andrea.giammar...@gmail.com 
>> <mailto:andrea.giammar...@gmail.com>>
>> wrote:
>>
>> > We are not under a unified VM (syntax not usable) and I cannot spot 
>> a single > benefit using two c...
>>
>> >

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to