Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Newbie trying to understand object oriented programming Date: Sat, 06 Feb 2016 16:01:08 +0100 Organization: PointedEars Software (PES) Lines: 69 Message-ID: <2195610.4cT6EPdiQK@PointedEars.de> References: <664696a0-3ce3-484c-aeaf-2fdf45781bb4@googlegroups.com> <9cydnYY0F6n_AyjLnZ2dnUU7-fOdnZ2d@westnet.com.au> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1454770869 26589 eJwNxckRwEAIA7CW2MHYoZyEo/8SNvoonIclMIjY2Nn/x2tpUVyzTKpbghJu71G6Q5gpNb+9C+8QlA== (6 Feb 2016 15:01:09 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 6 Feb 2016 15:01:09 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNzAkBACEIBMBKIrKwcZSnf4S7CTCmEKQfGI6NzXBvdUvhQzSGsvoET0+BQ83SYNnaccujHhP9F55y0Vj2AUX8FTI= Cancel-Lock: sha1:We6WM/qgIClXPSjpzjmEA8JaHHU= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BLJUBY5fDVv4TduVLYYXSaP3+thDGguLbeoFQAMcdrc0WjkhN3e05W1vcDBbwRJA== Xref: csiph.com comp.lang.javascript:29562 Stefan Ram wrote: > RobG writes: >> Within a function, *this* is assigned the value of an object > > That seems to be true. But it is only true in non-strict mode: | > typeof (function () { "use strict"; return this; }).call(2) | < "number" > It can be exploited to wrap a value into an object! What you are doing below is conversion, not “wrapping”. It is far simpler to call the function for a built-in type with the value to be converted as argument: > |< function toObject(){ return this; } > |> undefined > > |< typeof 2 > |> "number" > > |< typeof toObject.call( 2 ) > |> "object" new Number(2) does the same, but also works in strict mode. Still, creating such objects is unnecessary and recommended against as they cannot be modified and comparing against references to them tend to yield counter-intuitive results: var zeroValue = 0; var zeroObjectRef = new Number(0); if (zeroValue) { /* code here is not executed as 0 is converted to false */ } if (zeroObjectRef) { /* code here is executed as an object reference is converted to true */ } Therefore, JSHint issues a warning about that by default. >> You then create a *colour* property of this new instance and assign it >> the value "brown". > > BTW, ECMAScript uses the spelling »color« IIRC. There is no built-in “color” or “colour” property in ECMAScript. You are confusing the DOM (CSS) API with one family of programming languages that it can be used with (ECMAScript implementations). The OP is *not* using the DOM (CSS) API; they are creating a user-defined object, so they are free to use any property name in any language in any spelling that they want (“colour” is the spelling in British English), it should only refer to what they are storing as value (and it helps to choose an English name so that many people can understand your code easily, and you are less likely to have to use the bracket property accessor syntax). -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.