Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14709 > unrolled thread

why is this function used in conjunction with Object.create?

Started byvon kristoff <mrjcnicolas@gmail.com>
First post2012-06-27 08:09 -0700
Last post2012-06-29 16:25 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  why is this function used in conjunction with Object.create? von kristoff <mrjcnicolas@gmail.com> - 2012-06-27 08:09 -0700
    Re: why is this function used in conjunction with Object.create? John G Harris <john@nospam.demon.co.uk> - 2012-06-27 16:37 +0100
      Re: why is this function used in conjunction with Object.create? "Tom de Neef" <tdeneef@qolor.nl> - 2012-06-27 20:04 +0200
        Re: why is this function used in conjunction with Object.create? von kristoff <mrjcnicolas@gmail.com> - 2012-06-27 11:50 -0700
          Re: why is this function used in conjunction with Object.create? Andreas Bergmaier <andber93@web.de> - 2012-06-27 23:09 +0200
            Re: why is this function used in conjunction with Object.create? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-30 11:06 +0200
        Re: why is this function used in conjunction with Object.create? John G Harris <john@nospam.demon.co.uk> - 2012-06-28 11:10 +0100
    Re: why is this function used in conjunction with Object.create? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-06-29 16:25 -0700

#14709 — why is this function used in conjunction with Object.create?

Fromvon kristoff <mrjcnicolas@gmail.com>
Date2012-06-27 08:09 -0700
Subjectwhy is this function used in conjunction with Object.create?
Message-ID<a5d2849f-7e1d-45a4-9eb6-2d3b8eb4e820@googlegroups.com>
if (typeof Object.create !== "function") {
        Object.create = function (o) {
            function F() {}
            F.prototype = o;
            return new F();
        };
    }

there's the function .. do I really need to use this in my code? I dont understand what it is doing!

[toc] | [next] | [standalone]


#14713

FromJohn G Harris <john@nospam.demon.co.uk>
Date2012-06-27 16:37 +0100
Message-ID<V7o0z0Hsiy6PFw2w@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#14709
On Wed, 27 Jun 2012 at 08:09:05, in comp.lang.javascript, von kristoff
wrote:
>if (typeof Object.create !== "function") {
>        Object.create = function (o) {
>            function F() {}
>            F.prototype = o;
>            return new F();
>        };
>    }
>
>there's the function .. do I really need to use this in my code?

No, you don't need to use this in your code, ever. There are simpler and
clearer ways to create an object with a given prototype chain.


>I dont understand what it is doing!

Join the club! And I don't understand why anyone would want to publish
this code outside of an obfuscation contest.

  John
-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#14717

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-06-27 20:04 +0200
Message-ID<4feb4b76$0$6946$e4fe514c@news2.news.xs4all.nl>
In reply to#14713
"John G Harris" <john@nospam.demon.co.uk> schreef in bericht 
news:V7o0z0Hsiy6PFw2w@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD...
> On Wed, 27 Jun 2012 at 08:09:05, in comp.lang.javascript, von kristoff
> wrote:
>>if (typeof Object.create !== "function") {
>>        Object.create = function (o) {
>>            function F() {}
>>            F.prototype = o;
>>            return new F();
>>        };
>>    }
>>
>>there's the function .. do I really need to use this in my code?
>
> No, you don't need to use this in your code, ever. There are simpler and
> clearer ways to create an object with a given prototype chain.
>
>
>>I dont understand what it is doing!
>
> Join the club! And I don't understand why anyone would want to publish
> this code outside of an obfuscation contest.
>

Check Crockford's "JavaScript - The good parts". Page 22 at the bottom.
Tom

[toc] | [prev] | [next] | [standalone]


#14719

Fromvon kristoff <mrjcnicolas@gmail.com>
Date2012-06-27 11:50 -0700
Message-ID<2b1b8713-c63e-4edd-9c8c-3f1f3956706f@googlegroups.com>
In reply to#14717
On Wednesday, June 27, 2012 7:04:44 PM UTC+1, Tom de Neef wrote:
> "John G Harris" <john@nospam.demon.co.uk> schreef in bericht 
> news:V7o0z0Hsiy6PFw2w@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD...
> > On Wed, 27 Jun 2012 at 08:09:05, in comp.lang.javascript, von kristoff
> > wrote:
> >>if (typeof Object.create !== "function") {
> >>        Object.create = function (o) {
> >>            function F() {}
> >>            F.prototype = o;
> >>            return new F();
> >>        };
> >>    }
> >>
> >>there's the function .. do I really need to use this in my code?
> >
> > No, you don't need to use this in your code, ever. There are simpler and
> > clearer ways to create an object with a given prototype chain.
> >
> >
> >>I dont understand what it is doing!
> >
> > Join the club! And I don't understand why anyone would want to publish
> > this code outside of an obfuscation contest.
> >
> 
> Check Crockford's "JavaScript - The good parts". Page 22 at the bottom.
> Tom

OK - but it works without writing this code .. AND as far as i have understood JS the good parts on that page - you write that so you can ACTUALLY write Object.create ... but my javascript version allows me to write it without having to declare that function..

Hmmm?

[toc] | [prev] | [next] | [standalone]


#14723

FromAndreas Bergmaier <andber93@web.de>
Date2012-06-27 23:09 +0200
Message-ID<jsfsqp$rle$1@news.albasani.net>
In reply to#14719
von kristoff schrieb:
>
>>> if (typeof Object.create !== "function") {
>>>         Object.create = function (o) {
>>>             function F() {}
>>>             F.prototype = o;
>>>             return new F();
>>>         };
>>>     }
>
> ... but my javascript version allows me to write it without having to declare that function...

That just means you're one of the lucky. For those with older browsers 
and javascript versions that don't support it (then typeof Object.create 
is not "function", but "undefined"), this snippets adds (a part of) the 
functionality of the native Object.create (see also 
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/).

  Bergi

[toc] | [prev] | [next] | [standalone]


#14782

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-06-30 11:06 +0200
Message-ID<1611394.rOlob3pHFt@PointedEars.de>
In reply to#14723
Andreas Bergmaier wrote:

> von kristoff schrieb:
>>>> if (typeof Object.create !== "function") {
>>>>         Object.create = function (o) {
>>>>             function F() {}
>>>>             F.prototype = o;
>>>>             return new F();
>>>>         };
>>>>     }
>>
>> ... but my javascript version allows me to write it without having to
>> declare that function...
> 
> That just means you're one of the lucky. For those with older browsers
> and javascript versions that don't support it (then typeof Object.create

There is no javascript.  This feature was first specified in ECMAScript 
Edition 5:

<http://ecma-international.org/ecma-262/5.1/#sec-15.2.3.5>

(There is the value of fragment-linking the section number to the section 
itself: easy referencing.)

> is not "function", but "undefined"), this snippets adds (a part of) the
> functionality of the native Object.create (see also
> 
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/).

And here, e.g.:

<http://msdn.microsoft.com/en-us/library/ff925952(VS.94).aspx>
<http://www.opera.com/docs/specs/presto2.11/ecmascript/>

(I wonder, is there a deal or allowance between Oracle and its competitors 
that the latter can legally use the JavaScript trademark now?  It used to be 
Microsoft *JScript* for legal reasons, but Microsoft's is still a different 
ECMAScript implementation than Netscape/Mozilla.org JavaScript.  Same for 
Opera ECMAScript etc.)


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [next] | [standalone]


#14742

FromJohn G Harris <john@nospam.demon.co.uk>
Date2012-06-28 11:10 +0100
Message-ID<dE5gPqDE2C7PFw8u@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#14717
On Wed, 27 Jun 2012 at 20:04:44, in comp.lang.javascript, Tom de Neef 
wrote:
>"John G Harris" <john@nospam.demon.co.uk> schreef in bericht
>news:V7o0z0Hsiy6PFw2w@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD...
>> On Wed, 27 Jun 2012 at 08:09:05, in comp.lang.javascript, von kristoff
>> wrote:
>>>if (typeof Object.create !== "function") {
>>>        Object.create = function (o) {
>>>            function F() {}
>>>            F.prototype = o;
>>>            return new F();
>>>        };
>>>    }
>>>
>>>there's the function .. do I really need to use this in my code?
>>
>> No, you don't need to use this in your code, ever. There are simpler and
>> clearer ways to create an object with a given prototype chain.
>>
>>
>>>I dont understand what it is doing!
>>
>> Join the club! And I don't understand why anyone would want to publish
>> this code outside of an obfuscation contest.
>>
>
>Check Crockford's "JavaScript - The good parts". Page 22 at the bottom.

The author fails to explain why half-creating a new object is a useful 
thing to do. He assumes that the new object's own properties will be 
added in separate code. This is well known to be a bad thing to do in an 
OO program. Worse, if the program makes two objects of the same kind 
then you have a maintenance nightmare - remembering to update both 
pieces of code - even if you got it right in both places the first time.

   John

-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#14774

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2012-06-29 16:25 -0700
Message-ID<09136ab9-8734-4a52-b8b6-129ccef367f4@googlegroups.com>
In reply to#14709
On Wednesday, June 27, 2012 10:09:05 AM UTC-5, von kristoff wrote:
[...]
>         Object.create = function (o) {
>             function F() {}
>             F.prototype = o;
>             return new F();
>         };
>     }
[...]

This version would be faster and more memory efficient:

Object.create = (function(){
    function f(){}

    return function(o){
        f.prototype = o;
        return new f;
    }
})();

There is no need to create multiple copies of the function.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web