[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

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

twgray2007

11/11/2014 5:55:00 AM

"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) ?

4 Answers

Charles Lehner

11/12/2014 3:26:00 AM

0

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.

RobG

12/9/2014 1:15:00 PM

0

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

Thomas 'PointedEars' Lahn

12/9/2014 5:00:00 PM

0

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/obj...

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Cezary Tomczyk

12/9/2014 9:44:00 PM

0

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.ct...