为什么这个脚本不起作用? [英] Why wont this script work?

查看:74
本文介绍了为什么这个脚本不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网页,里面有一个脚本。


< html>


< head>


< title>自己做对象测试< / title>


< script>


功能信息(self,n,e){


功能showInfo(self,nam,ema){


document.write(" < hr>");


document.write("< b> Name:< / b>" + nam);

document.write("< b>电子邮件:< / b>" + ema);


}


self.name = n;


self.email = e;

self.show()= showInfo(self.name,self.email );


}


< / script>


< / head>


< body>


< script>


ali = info(" Ali" ;,al *@ali.com");


zainab = info(" Zainab"," za **** @ zainab.com");

ali.show();

zainab.show();


< / script>


< / body>


< / html>


我希望脚本生成两个对象:ali和zainab。这些对象

将有两个属性(名称和电子邮件)和一个方法(显示)。

show方法应该显示

对象的另外两个属性。


请帮忙。谢谢。 :)

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,nam, 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.name,self.email);

}

</script>

</head>

<body>

<script>

ali = info("Ali","al*@ali.com");

zainab = info("Zainab","za****@zainab.com");
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. :)

推荐答案

阿里说:

我有以下带有脚本的网页。
我希望脚本生成两个对象:ali和zainab。这些对象将具有两个属性(名称和电子邮件)和方法(显示)。
show方法应该显示
对象的其他两个属性。

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.




这是一个类,还是你想尝试自己学习Javascript吗?

您是从某种教科书或在线教程开始工作吗?



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写道:
Ali wrote:
< html>


此开始标记之前缺少DOCTYPE声明,例如


<!DOCTYPE html PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

" http://www.w3.org/TR/html4/loose.dtd">

< html>

[...]
< script>
^

类型属性丢失:


< script type =" text / javascript">

函数信息(self,n,e){

函数showInfo(self,nam,ema){


ECMAScript和实现中不允许使用嵌套函数。

但是,支持它在JavaScript 1.5中。可能

下面回答的问题是:为什么你认为这有必要?

document.write("< hr>");

document.write("< b> Name:< / b>" + nam);

document.write("< b> Email:< / b> ;" + ema);

}

self.name = n;


self指的是当前的window对象,因为这个对象实际上是与self属性的全局对象相同的
可用。

可能无法为其name属性赋值。大多数

当然你正在寻找`this.name''。

self.email = e;
self.show()= showInfo(self.name ,self.email);
^^

调用操作符只能在JavaScipt中使用右侧,
ECMAScript及其实现。

}

[...}
< script>


见上文。

ali = info(" Ali"," al*@ali.com");
zainab = info (QUOT;扎伊纳布"," ZA **** @ zainab.com");


应使用`var''关键字声明变量:


var ali = ...;

var zainab = ...;


假设info是对象的构造函数,正确的语法是


var ali = new info(" Ali"," al *@ali.com");

var ali =新信息(" Zainab"," za **** @ zainab.com");

ali.show();
zainab.show( );>


但是,如果show()应该是信息的方法。对象,正确的语法




函数信息(n,e)

{

this.show = function info_show()

{

//如果在HTML上下文中使用,则转义ETAGO

document.write("< hr>"

+"< b>名称:< \ / b>" + this.name

+"< b>电子邮件:< \ / b>" + this.email);

}


//考虑... = x || "英寸;默认值

this.name = n;

this.email = e;

}

$如果info_show()不包含闭包,则b $ b或更好:


函数信息(n,e)

{

//见上文

this.name = n;

this.email = e;

}


info.prototype.show = function info_show()

{

document.write("< hr>"

+"< b>名称:< \ / b>" + this.name

+"< b>电子邮件:< \ / b>" + this.email);

}


但是,document.write()不是输出信息的正确方法

之后文档已加载。它可能会暂时将信息添加到

文档中,也可能会覆盖整个文档,包括

其中定义的函数。向Google网上论坛寻求合适的解决方案。


为避免内置属性产生混淆和不良副作用,

良好的代码风格建议让构造函数标识符以一个

大写字母:


功能信息(...)

{

.. 。

}

[...]
我想让脚本制作两个对象:ali和zainab。这些对象将具有两个属性(名称和电子邮件)和方法(显示)。
show方法应该显示
对象的其他两个属性。
<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,nam, 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.name,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.com");
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.com");
var ali = new info("Zainab","za****@zainab.com");
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.




大多数情况下,你应该添加属性
第二个对象的
不显示(如预期的那样)。


不起作用是一个无用的错误描述。 [psf 4.11]


请在发布前阅读常见问题解答。

PointedEars

-

作为BugFree(tm),系列并没有那么好,我正在开始一个名为ItWorksForMe(tm)的新系列。其中这个新的(浏览器)是另一个

闪亮的例子......



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(tm)" of which this new (browser) is yet another
shining example...


在文章< 13 ******* *********@PointedEars.de> ;, Thomas''PointedEars''

Lahn< Po ********* @ web.de>写道


< snip>
In article <13****************@PointedEars.de>, Thomas ''PointedEars''
Lahn <Po*********@web.de> writes

<snip>
ECMAScript和实现中不允许使用嵌套函数。
然而,它在JavaScript 1.5中受支持。
Nested functions are not allowed in ECMAScript and implementations.
However, it is supported in JavaScript 1.5.



< snip>


ECMAScript v3允许使用它,已经超过4年b​​ $ b年。


John

-

John Harris


<snip>

It is allowed in ECMAScript v3, which has been around for over four
years.

John
--
John Harris


这篇关于为什么这个脚本不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆