[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

call function for times with different types

???

8/26/2014 5:59:00 AM

Hi, I was wondering in most of JavaScript programs. Is there any practical usage to call a function many times and each time with different types? Such kind of programs look awkward but it somehow follows the grammar and JavaScript is a dynamic typed language.

A simple example would be:

function foo(a) {
//do something here

}

foo(10); //numeric types
foo("test") //string type
foo(false); //boolean type
foo({"a":1}); //object type

Thanks.
11 Answers

Evertjan.

8/26/2014 7:22:00 AM

0

=?UTF-8?B?5ZCz5pme5rWp?= <shwu.cs98@gmail.com> wrote on 26 aug 2014 in
comp.lang.javascript:

> Hi, I was wondering in most of JavaScript programs.

I do not believe you have even seen a tiny fraction of Javascript
programme scripts.

> Is there any
> practical usage to call a function many times and each time with
> different types?

Ues, there is, because the programmer choose to make the function behave
that way.

> Such kind of programs look awkward

Those are not a "kind of programme" but just the whim of the programmer,
who likes a general function to behave slightly differently based on the
parameter input, in stead of trying to remember a lot of different
functions. This definitey may have its merits.

> but it somehow
> follows the grammar and JavaScript is a dynamic typed language.

No, not as a dynamic typed language and it does not "follow the grammar".

Javascript just makes this possible as a *weak* typed language.

<http://en.wikipedia.org/wiki/Strong_and_weak_...

> A simple example would be:
>
> function foo(a) {
> //do something here
>}

The a parameter is not even necessary,
as all functions have an internal parameter collection:

function myFunction() {
for(var i=0; i<arguments.length; i++)
alert('Good morning, ' + arguments[i]);
};

Also in this example the parameter types can differ:

myFunction(23, 'Mr. Jones', employee(77), true)

[..]
> Thanks.

Could you please ad a signature or at least a nickname,
discussing with a nobody who seems to be named
=?UTF-8?B?5ZCz5pme5rWp?= is rather combersome
and we would, be it slowly, come to know
whether you are a strong or weak type of programmer.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

8/26/2014 7:45:00 AM

0

schrieb Evertjan. wrote:

> =?UTF-8?B?5ZCz5pme5rWp?= <shwu.cs98@gmail.com> wrote on 26 aug 2014 in
> comp.lang.javascript:
>
> Could you please ad a signature or at least a nickname,
> discussing with a nobody who seems to be named
> =?UTF-8?B?5ZCz5pme5rWp?= is rather combersome
> and we would, be it slowly, come to know
> whether you are a strong or weak type of programmer.

It seems your newsreader can't properly handle encoded-words[1]. The OP
has posted as 吳æ??浩 <shwu.cs98@gmail.com>, which is perfectly fine.

[1] <http://tools.ietf.org/html/r...

--
Christoph M. Becker

Evertjan.

8/26/2014 8:11:00 AM

0

"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 aug 2014 in
comp.lang.javascript:

> schrieb Evertjan. wrote:
>
>> =?UTF-8?B?5ZCz5pme5rWp?= <shwu.cs98@gmail.com> wrote on 26 aug 2014 in
>> comp.lang.javascript:
>>
>> Could you please ad a signature or at least a nickname,
>> discussing with a nobody who seems to be named
>> =?UTF-8?B?5ZCz5pme5rWp?= is rather combersome
>> and we would, be it slowly, come to know
>> whether you are a strong or weak type of programmer.
>
> It seems your newsreader can't properly handle encoded-words[1]. The OP
> has posted as 吳æ??浩 <shwu.cs98@gmail.com>, which is perfectly fine.

I don't think ³æ??浩 is perfectly fine, usenet stipulates using ASCII for
addresslines, so much for your "properly".

However, dear Christoph, even having a fine addressline, quod non, does not
in the least diminish the value of my request for a signature or at least a
nickname.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

8/26/2014 11:25:00 AM

0

Evertjan. wrote:

> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 26 aug 2014 in
> comp.lang.javascript:
>
>> schrieb Evertjan. wrote:
>>
>>> =?UTF-8?B?5ZCz5pme5rWp?= <shwu.cs98@gmail.com> wrote on 26 aug 2014 in
>>> comp.lang.javascript:
>>>
>>> Could you please ad a signature or at least a nickname,
>>> discussing with a nobody who seems to be named
>>> =?UTF-8?B?5ZCz5pme5rWp?= is rather combersome
>>> and we would, be it slowly, come to know
>>> whether you are a strong or weak type of programmer.
>>
>> It seems your newsreader can't properly handle encoded-words[1]. The OP
>> has posted as 吳�浩 <shwu.cs98@gmail.com>, which is perfectly fine.
>
> I don't think ³æ��浩 is perfectly fine, usenet stipulates using ASCII for
> addresslines, so much for your "properly".

It seems you are referring to RFC 1036 section 2.1.1. However, RFC 1036
is obsoleted by RFC 5536 and 5537, and RFC 5536 section 3.1.2 specifies:

| The From header field is the same as that specified in Section 3.6.2
| of [RFC5322], with the added restrictions detailed above in
| Section 2.2.

I have not been able to find any restriction on ASCII characters,
besides that non ASCII characters have to be encoded, what they have
been in the OP.

However, if you post to Usenet, you should be aware that the default
Content-Type is

Content-type: text/plain; charset=us-ascii

So if you are using non ASCII characters, you should configure your
newsreader to add an appropriate Content-Type header field.

> However, dear Christoph, even having a fine addressline, quod non, does not
> in the least diminish the value of my request for a signature or at least a
> nickname.

Well, the name has been already given in the "addressline"--there is no
*need* to duplicate it.

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

8/26/2014 11:59:00 AM

0

吳æ??浩 wrote:

> Hi, I was wondering in most of JavaScript programs. Is there any practical
> usage to call a function many times and each time with different types?

Yes, but you need to be careful with such overloading (many people, library
authors included, are not). It is usually important that you are able to
tell different types of arguments apart in order to handle them differently;
this excludes, for example, overloading with references to host objects.
Also, a one-size-fits-all function quickly becomes hard to maintain. So be
slow to do that.

> Such kind of programs look awkward but it somehow follows the grammar

It follows the respective ECMAScript grammar exactly or it would not
execute. For ECMAScript Ed. 5.1 the relevant productions are

CallExpression : MemberExpression Arguments

Arguments : ( ArgumentList )

> and JavaScript is a dynamic typed language.

The correct term here is â??dynamic type-checkingâ? (not: â??weak typingâ?, which
is underdefined). The type of a symbol, like a variable or property, is
checked when it is accessed, and implicit conversion is being done if
necessary for an operation.

Whether that term applies to JavaScript depends on what you mean by
â??JavaScriptâ?. Contrary to popular belief, there is no single programming
language to fit them all. There are several implementations of Editions of
ECMAScript which are different one one another (as allowed by the
Specification). For example, there are implementations that may bear the
â??JavaScriptâ? name but also support static type-checking. There you can
declare the type for a formal parameter:

function f (arg1 : int, arg2 : boolean)
{
}

However, those are, at least up to now, not used in an HTML context.

Google Groups is borken with regard to Network News/Usenet. You should use
it only for searching newsgroups, not for posting to them. Use a newsreader
instead.

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

Thomas 'PointedEars' Lahn

8/26/2014 12:56:00 PM

0

吳æ??浩 wrote:

> Hi, I was wondering in most of JavaScript programs. Is there any practical
> usage to call a function many times and each time with different types?

Yes, but you need to be careful with such overloading (many people, library
authors included, are not). It is usually important that you are able to
tell different types of arguments apart in order to handle them differently;
this excludes, for example, overloading with references to host objects.
Also, a one-size-fits-all function quickly becomes hard to maintain. So be
slow to do that.

> Such kind of programs look awkward but it somehow follows the grammar

It follows the respective ECMAScript grammar exactly or it would not
execute. For ECMAScript Ed. 5.1, the relevant productions are

CallExpression : MemberExpression Arguments

Arguments : ( ArgumentList )

ArgumentList :
ArgumentList , AssignmentExpression
ArgumentList : AssignmentExpression

As you can see, there is no limitation as to what can be passed to a
function except that the argument needs to be produced by
AssignmentExpression.

> and JavaScript is a dynamic typed language.

The correct term here is â??dynamic type-checkingâ? (not: â??weak typingâ?, which
is underdefined). The type of a symbol, like a variable or property, is
checked when it is accessed, and implicit conversion is being done if
necessary for an operation.

Whether that term applies to JavaScript depends on what you mean by
â??JavaScriptâ?. Contrary to popular belief, there is no single programming
language to fit them all. There are several implementations of Editions of
ECMAScript which are different from one another (as allowed by the
â??Conformanceâ? section of the Specification). For example, there are
implementations that may bear the â??JavaScriptâ? name but also support static
type-checking (and other data types). There you can declare the type for a
formal parameter:

function f (arg1 : int, arg2 : boolean)
{
}

However, those are, at least up to now, not used in an HTML context.

See the ES Matrix (URI below) for details.


Google Groups is borken with regard to Network News/Usenet. You should use
it only for searching newsgroups, not for posting to them. Use a newsreader
instead, like KNode, Thunderbird, or SeaMonkey Mail/News.

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

Thomas 'PointedEars' Lahn

8/26/2014 3:25:00 PM

0

Christoph M. Becker wrote:

> Evertjan. wrote:
>> "Christoph M. Becker" [â?¦] wrote [â?¦]:
> However, if you post to Usenet, you should be aware that the default
> Content-Type is
>
> Content-type: text/plain; charset=us-ascii
>
> So if you are using non ASCII characters, you should configure your
^ in the message *body*

> newsreader to add an appropriate Content-Type header field.

JFTR: The â??Content-Typeâ? header field only applies to the message body.

>> However, dear Christoph, even having a fine addressline, quod non, does
>> not in the least diminish the value of my request for a signature or at
>> least a nickname.
>
> Well, the name has been already given in the "addressline"--there is no
> *need* to duplicate it.

However, since English, not Chinese, has become the /lingua franca/ of
computer science (and technology in general), it is be prudent to specify
the Hanyu Pinyin transcription of the name as well (or instead of the real
name) in this international newsgroup. In this case, with Google Translate
recognizing the name as Chinese, it would probably be â??Wu Xi Haoâ? (as
suggested there). â??Wuâ? being a common Chinese family name, it would be
customary to write â??WU Xi Haoâ? to signify that.

I know first-hand from my flatmates at the university back in 2003 that at
least some Chinese like to choose an English first name on the Net instead
of their real first name which might be difficult to pronounce properly, and
even more difficult to write, by people whose native language is not one of
the Chinese languages. The traditional order of the given name is then
reversed, for example â??Robert Wuâ? (which, by serendipity, is also the name
of a Chinese-American actor and producer).

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

Denis McMahon

8/26/2014 3:47:00 PM

0

On Tue, 26 Aug 2014 13:25:05 +0200, Christoph M. Becker wrote:

> It seems you are referring to RFC 1036 section 2.1.1. However, RFC 1036
> is obsoleted by RFC 5536 and 5537, and RFC 5536 section 3.1.2 specifies:

I think you'll find that until RFCs 5536 and 5537 are actually standards,
rather than 5 year old proposed standards, they don't obsolete 1036.

--
Denis McMahon, denismfmcmahon@gmail.com

Thomas 'PointedEars' Lahn

8/26/2014 4:22:00 PM

0

Denis McMahon wrote:

> On Tue, 26 Aug 2014 13:25:05 +0200, Christoph M. Becker wrote:
>> It seems you are referring to RFC 1036 section 2.1.1. However, RFC 1036
>> is obsoleted by RFC 5536 and 5537, and RFC 5536 section 3.1.2 specifies:
>
> I think you'll find that until RFCs 5536 and 5537 are actually standards,
> rather than 5 year old proposed standards, they don't obsolete 1036.

Your logic is flawed and your research method is lacking.

You will find that RFCs 5536, 5537, and 5322 are *Proposed* Standards and a
*Draft* Standard, respectively â?? RFCs on the IETF Standards Track (not
Internet standards yet; those have an STD number, too) â??, and that,
regardless of that, the former two *explicitly* obsolete RFC 1036 and the
latter obsoletes RFC 2822. As such, they bear the informal significance of
â??quasi-standardâ? or â??de-facto standardâ? â?? something to be observed if one is
to avoid interoperability problems.

It was about time in 2009, with the so-called â??Son-of-RFC-1036â? working
draft being the implemented de-facto standard for Network News for the
previous 9 years at least (it was the de-facto standard when I first used
Usenet).

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

Thomas 'PointedEars' Lahn

8/26/2014 11:41:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> However, since English, not Chinese, has become the /lingua franca/ of
> computer science (and technology in general), it is be prudent to specify
> the Hanyu Pinyin transcription of the name as well (or instead of the real
> name) in this international newsgroup. In this case, with Google
> Translate recognizing the name as Chinese, it would probably be â??Wu Xi
> Haoâ? (as suggested there). â??Wuâ? being a common Chinese family name, it
> would be customary to write â??WU Xi Haoâ? to signify that.

I have just learned that although there are three characters (吳æ??浩), the
pinyin romanization should not be written â??Wu Xi Haoâ?, but (without tone
marks) probably â??Wu Xihaoâ? or â??Wu Xiaoâ?. Another possibility is the older
Wade-Giles system, in which it could be (without tone marks) â??Wu Xi-Haoâ??.

<https://en.wikipedia.org/wiki/Chinese_given_name#Descr...


F'up2 alt.chinese.text

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