I have the following web page with a script in it.
<html>
<head>
<title>Make Your Own Objects test</title>
<script>
function info(self,n,e) {
function showInfo(self,n am, ema) {
document.write( "<hr>");
document.write( "<b>Name: </b>"+nam);
document.write( "<b>Email: </b>"+ema);
}
self.name = n;
self.email= e;
self.show() = showInfo(self.n ame,self.email) ;
}
</script>
</head>
<body>
<script>
ali = info("Ali","al* @ali.com");
zainab = info("Zainab"," za****@zainab.c om");
ali.show();
zainab.show();
</script>
</body>
</html>
I want the script to make two objects: ali and zainab. These objects
are to have two properties (name and email) and a method (show). The
show method is supposed to display the other two properties of the
object.
Please Help. Thank You. :) 12 2162
Ali said: I have the following web page with a script in it.
I want the script to make two objects: ali and zainab. These objects are to have two properties (name and email) and a method (show). The show method is supposed to display the other two properties of the object.
Is this for a class, or are you trying to learn Javascript on your own?
Are you working from some sort of textbook or online tutorial?
Ali wrote: <html>
The DOCTYPE declaration is missing prior to this start tag, e.g.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
[...] <script>
^
The "type" attribute is missing:
<script type="text/javascript">
function info(self,n,e) {
function showInfo(self,n am, ema) {
Nested functions are not allowed in ECMAScript and implementations .
However, it is supported in JavaScript 1.5. The question probably
answered below is: Why do you consider it necessary here?
document.write( "<hr>");
document.write( "<b>Name: </b>"+nam);
document.write( "<b>Email: </b>"+ema);
}
self.name = n;
`self' refers to the current `window' object as this object is virtually
identical with the global object where the `self' property is available.
Assigning a value to its `name' property may not be possible. Most
certainly you are looking for `this.name'.
self.email= e; self.show() = showInfo(self.n ame,self.email) ;
^^
The call operator may only be used right hand side in JavaScipt,
ECMAScript and its implementations .
}
[...} <script>
See above.
ali = info("Ali","al* @ali.com"); zainab = info("Zainab"," za****@zainab.c om");
Variables should be declared, using the `var' keyword:
var ali = ...;
var zainab = ...;
Assuming that "info" is the constructor of an object, the proper syntax is
var ali = new info("Ali", "al*@ali.co m");
var ali = new info("Zainab"," za****@zainab.c om");
ali.show(); zainab.show();>
However, if show() ought to be a method of "info" objects, the proper syntax
is
function info(n, e)
{
this.show = function info_show()
{
// escape ETAGO if used in HTML context
document.write( "<hr>"
+ "<b>Name: <\/b>" + this.name
+ "<b>Email: <\/b>" + this.email);
}
// consider ... = x || ""; for a default value
this.name = n;
this.email = e;
}
or better if info_show() does not contain a closure:
function info(n, e)
{
// see above
this.name = n;
this.email = e;
}
info.prototype. show = function info_show()
{
document.write( "<hr>"
+ "<b>Name: <\/b>" + this.name
+ "<b>Email: <\/b>" + this.email);
}
However, document.write( ) is not a proper method to output information
after the document has been loaded. It may add information to the
document temporarily or it may overwrite the entire document, including
the functions defined therein. Ask Google Groups for proper solutions.
To avoid confusion and undesired side effects with built-in properties,
good code style recommends to let constructor identifiers begin with an
uppercase letter:
function Info(...)
{
...
}
[...] I want the script to make two objects: ali and zainab. These objects are to have two properties (name and email) and a method (show). The show method is supposed to display the other two properties of the object.
And most certainly, you should have added that the properties
of the second object are not displayed (as expected).
"Does not work" is a useless error description. [psf 4.11]
Please read the FAQ before posting.
PointedEars
--
As the "BugFree(tm )" series didn't turn out so well, i'm starting a new
series called "ItWorksForMe(t m)" of which this new (browser) is yet another
shining example...
In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars'
Lahn <Po*********@we b.de> writes
<snip> Nested functions are not allowed in ECMAScript and implementations . However, it is supported in JavaScript 1.5.
<snip>
It is allowed in ECMAScript v3, which has been around for over four
years.
John
--
John Harris
John G Harris wrote: In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn <Po*********@we b.de> writes
Please stop posting attribution novels; the reasons have been explained
(to you) several times before. Nested functions are not allowed in ECMAScript and implementations . However, it is supported in JavaScript 1.5.
It is allowed in ECMAScript v3, which has been around for over four years.
I'd be happy to be wrong here and read about the required productions
from the ECMAScript 3 grammar in a posting of you.
PointedEars
--
Dance, Mozilla, Dance!
Thomas 'PointedEars' Lahn wrote: John G Harris wrote:
In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn <Po*********@we b.de> writes
Please stop posting attribution novels; the reasons have been explained (to you) several times before.
Considering that you are the only one that explains it, and you
generally and 100% explain it wrongly, thats just more of your unfounded
garbage spouting.
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
On Sun, 17 Oct 2004 23:26:27 +0200, Thomas 'PointedEars' Lahn
<Po*********@we b.de> wrote: John G Harris wrote:
In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn <Po*********@we b.de> writes
[snip] Nested functions are not allowed in ECMAScript and implementations . However, it is supported in JavaScript 1.5.
It is allowed in ECMAScript v3, which has been around for over four years.
I'd be happy to be wrong here and read about the required productions from the ECMAScript 3 grammar in a posting of you.
Will I do?
Unless I'm missing something major (quite possible as I don't have this
thread in full), you seem to be suggesting that
function a() {
function b() {
}
}
is illegal. Is that correct? Well, in a code example, section 13.2 -
Creating Function Objects, they have written almost precisely the above.
If you want the associated grammar:
FunctionDeclara tion :
function Identifier ( FormalParameter Listopt ) { FunctionBody }
FunctionBody :
SourceElements
SourceElements :
SourceElement
SourceElements SourceElement
SourceElement :
Statement
FunctionDeclara tion
That suggests to me that it is perfectly legal to nest functions to any
degree. I haven't checked to any great extent to see if the text notes
limitations or exceptions, but I doubt there are any.
One thing I had noticed in the grammar is that you may not place function
declarations directly within a statement. As far as I can see, function
statements may only occur in:
- The program source at the "root" level.
- Other functions (expressions or statements).
- An eval() argument.
That would make something like:
if(...) {
function myFunction() {
}
}
illegal. However, a quick test shows that (at least some) browsers support
it. Why you'd do something like this, though, is another question.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Michael Winter wrote: On Sun, 17 Oct 2004 23:26:27 +0200, Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote: John G Harris wrote: In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn <Po*********@we b.de> writes
<snip> Nested functions are not allowed in ECMAScript and implementations . However, it is supported in JavaScript 1.5.
Nested function declarations and function expressions are supported by
Netscape 4 with JavaScript 1.3 and IE 4 (approx: JScript 2), about the
oldest browsers that remain viable as scriptable Internet user agents
(certainly the oldest considered for active support in a commercial
context, and often not then).
As is not unusual for such documents, the 3rd edition of ECMAScript was
formalising behaviour already common to implementations .
It is allowed in ECMAScript v3, which has been around for over four years.
And has been a standard feature of browser scripting engine
implementations considerably longer.
I'd be happy to be wrong here and read about the required productions from the ECMAScript 3 grammar in a posting of you. Will I do?
Unless I'm missing something major (quite possible as I don't have this thread in full), you seem to be suggesting that
function a() { function b() { } }
is illegal. Is that correct? Well, in a code example, section 13.2 - Creating Function Objects, they have written almost precisely the above.
If nested functions were not explicitly supported there would not be
much point in ECMA 262 (3rd edition) going into quite so much detail
about how the scope chain and the internal [[Scope]] properties are
handled, as there would be no ECMAScript circumstances where it would
make any difference (which is probably why the second edition can get
away with defining only one form of scope chain for any execution
context).
If you want the associated grammar:
FunctionDeclara tion : function Identifier ( FormalParameter Listopt ) { FunctionBody }
FunctionBody : SourceElements
SourceElements : SourceElement SourceElements SourceElement
SourceElement : Statement FunctionDeclara tion
Yes, function bodies may explicitly contain function declarations, and
function expressions may appear anywhere that a MemberExpressio n can be
used:-
| 11.2 Left-Hand-Side Expressions
| Syntax
| MemberExpressio n :
| PrimaryExpressi on
| FunctionExpress ion
| MemberExpressio n [ Expression ]
| MemberExpressio n . Identifier
| new MemberExpressio n Arguments
That suggests to me that it is perfectly legal to nest functions to any degree. I haven't checked to any great extent to see if the text notes limitations or exceptions, but I doubt there are any.
One thing I had noticed in the grammar is that you may not place function declarations directly within a statement. As far as I can see, function statements may only occur in:
- The program source at the "root" level. - Other functions (expressions or statements). - An eval() argument.
That would make something like:
if(...) { function myFunction() { } }
illegal. However, a quick test shows that (at least some) browsers support it.
More involved tests would show extremely inconsistent handling of that
code. The production rules forbid a function declaration form appearing
within a block statement so the only valid ECMAScript interpretation of
that is as a function expression with optional identifier, and just
asserted (never assigned to anything or called, so a pointless
expression).
In practice browsers may take it as a function declaration (unless
explicitly parenthesised, which makes it unambiguously an expression)
and act on it during variable instantiation (effectively removing it
form its block context), and Mozilla browsers treat it as an expression
but erroneously (by ECMA 262) leak the optional identifier into the
containing scope (producing an effect indistinguishab le form a
conditional function declaration (if such existed in ECMAScript).
Why you'd do something like this, though, is another question.
You wouldn't, but an inner function expression, conditionally evaluated
and assigned, is completely normal.
Richard.
Richard Cornford wrote: Michael Winter wrote: FunctionDeclara tion : function Identifier ( FormalParameter Listopt ) { FunctionBody }
FunctionBody : SourceElements
SourceElements : SourceElement SourceElements SourceElement
SourceElement : Statement FunctionDeclara tion Yes, function bodies may explicitly contain function declarations,
ACK, but only at first level.
and function expressions may appear anywhere that a MemberExpressio n can be used:-
| 11.2 Left-Hand-Side Expressions | Syntax | MemberExpressio n : | PrimaryExpressi on | FunctionExpress ion | MemberExpressio n [ Expression ] | MemberExpressio n . Identifier | new MemberExpressio n Arguments
However, the flaw of the latter argument (if that is what it is supposed
to be) is that a FunctionExpress ion is not a FunctionDeclara tion and
vice-versa.
PointedEars
JRS: In article <20************ ****@PointedEar s.de>, dated Sun, 17 Oct
2004 23:26:27, seen in news:comp.lang. javascript, Thomas 'PointedEars'
Lahn <Po*********@we b.de> posted : John G Harris wrote:
In article <13************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn <Po*********@we b.de> writes
Please stop posting attribution novels; the reasons have been explained (to you) several times before.
They are invalid reasons. There is no support for them in accepted
Usenet standards and norms; and Usefor WIP allows a full attribution
such as I use.
You may recall that the German nation got rather a bad name in the early
1870s, the 1910s, and in 1933-45. It seems unwise of you to attempt to
repeat the situation single-handedly.
Given your rather silly habit of reviving ancient threads, threads that
the saner readers of c.l.j have long lost interest in, threads that are
no longer conveniently accessible to those using off-line newsreaders,
putting the date of the previous article in an attribution would be a
convenience to users of the group -- it is not always possible to
determine the date of a previous article from its message-ID in
References, and, when it is possible, many will not know how to do it,
and others find it inconvenient to understand.
In brief, pleas cease your Schickelgrubing . Nested functions are not allowed in ECMAScript and implementations . However, it is supported in JavaScript 1.5.
It is allowed in ECMAScript v3, which has been around for over four years.
I'd be happy to be wrong here and read about the required productions from the ECMAScript 3 grammar in a posting of you.
They are supported in MSIE4, which Flanagan says is JS 1.2; that is over
six years old.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Oliver Spiesshofer |
last post by:
Hi,
I have a script that calls an fopen() on an external URL.
I can run the script from the server as an url fine.
When however the script should be run from crontab, it does not work.
I get
Warning: fopen(): URL file-access is disabled in the server configuration
in
|
by: Catherine |
last post by:
I am having a problem viewing asp pages on iis version
5.1 xp pro.
HTML pages are viewable on http://localhost but .asp
pages are not. I have created a test program called
timetest.asp with the following code:
<html>
<head>
<title>Test ASP</title>
|
by: Allen Thompson |
last post by:
sorry for the simple question, haven't done this in a while. when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as 721
instead of the answer I want which is 28. what am I doing wrong.
hanks -Allen Thompson
<html>
<body>
<script...
|
by: Steve Westwood |
last post by:
I have a WebForm button. Depending on calculation I wont to send a popup
message to the web page. I am not sure of the approach. From the server
side code I don't seem to be able to access the client side script. Can
anyone help?
I am using the VS .net C# environment
|
by: news frontiernet.net |
last post by:
I have a project that uses mouse-over JS script to show and hide layers that
works well in MSIE 6.0 and Opera.
But, it does not work in NS nor Mozilla.
It is here: http://www.wgtn.net/Business/category_layer.htm
A mouse-over of any of the letters in that left vertical panel of letters
shoud trigger a JS script that shows the...
| |
by: Dufus |
last post by:
<a id="MyDataList__ctl1_edit_Button" NAME="edit_Button"
OnMousedown="EnableValidators('editGroup_');" href="javascript:{if
(typeof(Page_ClientValidate) != 'function' || Page_ClientValidate())
__doPostBack('MyDataList$_ctl1$edit_Button','')} ">Edit</a>
In the above example, the function 'EnableValidators' does not fire. Has it
got...
|
by: Ronan Dodworth |
last post by:
Hi there
I'm having a little bit of a problem with my customvalidator control.
The problem is the javascript runs fine on my local webserver IIS but
not when I post it to the web hosting server. As it is client side
scripting I'm suprised that this is the case as I'm using the same
browser IE6 and the same machine to access both. I've...
|
by: squash |
last post by:
The following code works fine in Firefox/Netscape but wont work in IE.
I suspect the problem is with one of these two simple functions. If
there is no obvious error Ill paste the entire code.
///////////////////////////////////////////////////////
function get_last_updated() {
http.open( 'get', url );
http.onreadystatechange =...
|
by: theJonster |
last post by:
Hi
I am embedding a swf object. I try:
<script type="text/javascript" src="../scripts/swfobject.js"></script>
<script type="text/javascript">
var flashvars = {
linktarget: "_top",
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |