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


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

Beginner's question: How does this function definition work?

Started bytwgray2007@gmail.com
First post2014-11-10 21:54 -0800
Last post2014-12-09 17:59 +0100
Articles 5 — 5 participants

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


Contents

  Beginner's question:  How does this function definition work? twgray2007@gmail.com - 2014-11-10 21:54 -0800
    Re: Beginner's question:  How does this function definition work? Charles Lehner <est.cel@celehner.com> - 2014-11-11 22:26 -0500
      Re: Beginner's question:  How does this function definition work? RobG <rgqld@iinet.net.au> - 2014-12-09 23:15 +1000
        Re: Beginner's question:  How does this function definition work? cezary.tomczyk@gmail.com - 2014-12-09 13:43 -0800
      Re: Beginner's question:  How does this function definition work? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-09 17:59 +0100

#26108 — Beginner's question: How does this function definition work?

Fromtwgray2007@gmail.com
Date2014-11-10 21:54 -0800
SubjectBeginner's question: How does this function definition work?
Message-ID<6fae7412-601f-4fda-8739-35cc865cc36d@googlegroups.com>
  "init" : function init(x, y, settings) {
            this.value = 1000;
            this.parent(x, y, settings);
  }

I just don't get it. I know : is equivalent to '='., so is it same as:

var init = function(x, y, settings) ?

[toc] | [next] | [standalone]


#26120

FromCharles Lehner <est.cel@celehner.com>
Date2014-11-11 22:26 -0500
Message-ID<20141111222613.2d818647@cel.eternal-september.org>
In reply to#26108
On Mon, 10 Nov 2014 21:54:50 -0800 (PST)
twgray2007@gmail.com wrote:

>   "init" : function init(x, y, settings) {
>             this.value = 1000;
>             this.parent(x, y, settings);
>   }
> 
> I just don't get it. I know : is equivalent to '='., so is it same as:
> 
> var init = function(x, y, settings) ?
> 

In this case : is used only when defining a property in an object literal. This is often used with functions when creating a prototype object, e.g.:

	Thing.prototype = {
		init: function() {
			/* ... */
		}
	};

Using = assigns the function value to a variable, rather than as a property of an object.

If defining a function with a colon not in an object literal doesn't produce an error, then it is actually getting parsed as a label, which is an unrelated, obscure feature of the language having to do with switch statements.

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


#26289

FromRobG <rgqld@iinet.net.au>
Date2014-12-09 23:15 +1000
Message-ID<fv2dnYIrgd5_aBvJnZ2dnUU7-TOdnZ2d@westnet.com.au>
In reply to#26120
On 12/11/2014 1:26 pm, Charles Lehner wrote:
> On Mon, 10 Nov 2014 21:54:50 -0800 (PST) twgray2007@gmail.com wrote:
>
>> "init" : function init(x, y, settings) { this.value = 1000;
>> this.parent(x, y, settings); }
>>
>> I just don't get it. I know : is equivalent to '='., so is it same
>> as:
>>
>> var init = function(x, y, settings) ?
>>
>
> In this case : is used only when defining a property in an object
> literal. This is often used with functions when creating a prototype
> object, e.g.:
>
> Thing.prototype = { init: function() { /* ... */ } };
>
> Using = assigns the function value to a variable, rather than as a
> property of an object.
>
> If defining a function with a colon not in an object literal doesn't
> produce an error, then it is actually getting parsed as a label,
> which is an unrelated, obscure feature of the language having to do
> with switch statements.

Speaking of obscure features, I stumbled across a script file called 
sp.ui.rte.js in SharePoint (it's over 14,000 lines of code) that starts 
with:

   function ULSNVe() {
     ...
   }

Then every function expression and declaration thereafter contains, at 
the top, the statement:

     ULSNVe: ;

which would seem to be a label. I can't fathom why anyone would do that.


-- 
Rob

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


#26297

Fromcezary.tomczyk@gmail.com
Date2014-12-09 13:43 -0800
Message-ID<0c1a8f05-45fb-46f4-801b-635d9fd45403@googlegroups.com>
In reply to#26289
On 9 december 2014 14:15:23 UTC+1 user RobG wrote:
[...]
> Speaking of obscure features, I stumbled across a script file called 
> sp.ui.rte.js in SharePoint (it's over 14,000 lines of code) that starts 
> with:
> 
>    function ULSNVe() {
>      ...
>    }
> 
> Then every function expression and declaration thereafter contains, at 
> the top, the statement:
> 
>      ULSNVe: ;
> 
> which would seem to be a label. I can't fathom why anyone would do that.

I think above code is a result of compiling other language to JavaScript. That's my assumption.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

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


#26292

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-12-09 17:59 +0100
Message-ID<5703570.yfUBlUlCX7@PointedEars.de>
In reply to#26120
Charles Lehner wrote:

> Thing.prototype = {
> init: function() {
> /* ... */
> }
> };
> 
> Using = assigns the function value to a variable, rather than as a
> property of an object.

Incorrect.

The “=” operator assigns the evaluation result of the expression right-hand 
side (RHS) to the evaluation result of the expression left-hand side (LHS).

If the LHS matches the property accessor syntax, a property with the value 
is created or an existing property value is overwritten by the RHS (unless 
prevented/transmuted by a setter, which is triggered by this if there is 
any).

If the LHS is an identifier, Identifier Resolution takes place in order to 
determine whether an object in the scope chain has such a property or there 
is a variable with the identifier as its identifier in scope; if there is no 
such object and variable, and the code is not strict mode code, the global 
object is augmented with a property with the RHS (otherwise a ReferenceError 
exception is thrown).

If the LHS is a /VariableDeclaration/ a variable is created in the local 
scope and assigned the value RHS; if a variable with the same identifier has 
already been declared, the previous value is overwritten.

Here, “prototype” is a *property* of the object referred to by “Thing”, as 
indicated by the dot property accessor notation LHS.  It is assigned a 
reference to an Object instance that has been created with the Object 
Initialiser RHS.  The Object Initialiser contains a definition for an “init” 
property which is assigned, upon initialization, a reference to a Function 
instance created by function expression, through which it becomes a method 
of the aforementioned Object instance.

Since the “prototype” property of Function instances is special, all objects 
that are created by calling the Function instance referred to by “Thing” as 
a constructor inherit the properties of the referred Object instance through 
the prototype chain.

This is not the recommended way to define a prototype.  By doing this, 
instances created through the constructor have the object referred to by 
“Object”, not the object referred by “Thing” referred by the value of their 
“constructor” property.  This can only be fixed by creating a “constructor” 
property on the prototype with the appropriate value; that property is 
enumerable by default and shows up in for-in iteration.  Only in 
implementations of ECMASCript Edition 5 and later could this be prevented in 
a safe, standards-compliant way.  Therefore, the object referred to by the 
predefined value of the prototype property should be augmented instead.  
Functions and methods can be written to reduce the amount of code that one 
needs to write, however one needs to be aware that code analyzers need 
additional hints then.

Either

  Thing.prototype.init = function (…) {
    // …
  };

  …

or, for example,

  Thing.extend(…, {
    /** @memberOf Thing.prototype */
    init: function (…) {
      …
    }
  });

whereas extend() would be a user-defined method like 
Function.prototype.extend() in

<http://Pointedears.de/wsvn/JSX/trunk/object.js>

Using such a method has the additional advantage that you can easily set up 
longer prototype chains to emulate class-based inheritance, which allows for 
better code reuse.

> If defining a function with a colon not in an object literal doesn't
> produce an error, then it is actually getting parsed as a label, which is
> an unrelated, obscure feature of the language having to do with switch
> statements.

Incorrect.

A label-like expression in an Object Initialiser is never parsed as a label.  
Instead, the Object Initialiser-like expression is actually parsed as a 
/Block/ statement, and the IdentifierName followed by the colon as according 
to the /LabelledStatement/ production.  This is only possible if the 
expression RHS of the colon can be produced by the /Statement/ production of 
the grammar.

Labelled statement have nothing to do with “switch” statements in 
particular.  The expression LHS of switch statement’s colons is _not_ a 
label, but an AssignmentExpression which is matched by loose comparison 
(Abstract Equality Comparison Algorithm).

As for “obscure”, that is a matter of opinion.  ISTM that those who did not 
have the basic knowledge above should keep to themselves their opinion as to 
what is “obscure” in the discussed programming language.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


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


csiph-web