JavaScript constructorÊôÐÔ
Definition and Usage
¶¨ÒåÓëÓ÷¨The constructor property is a reference to the function that created an object.
constructorÊôÐÔÊÇËù½¨Á¢¶ÔÏóµÄº¯Êý²Î¿¼Syntax
Óï·¨object.constructor
Example 1
¾ÙÀý
In this example we will show how to use the constructor property:
ÔÚÕâ¸ö¾ÙÀýÖÐÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓÃconstructorÊôÐÔ£º
<script type="text/javascript">
var test=new Array()
if (test.constructor==Array)
{document.write("This is an Array")}
if (test.constructor==Boolean)
{document.write("This is a Boolean")}
if (test.constructor==Date)
{document.write("This is a Date")}
if (test.constructor==String)
{document.write("This is a String")}
</script>
The output of the code above will be:
Êä³ö½á¹ûΪ£º
This is an Array
Example 2
¾ÙÀý2
In this example we will show how to use the constructor property:
ÔÚÕâ¸ö¾ÙÀýÖÐÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓÃconstructorÊôÐÔ£º
<script type="text/javascript">
function employee(name,jobtitle,born)
{
this.name=name
this.jobtitle=jobtitle
this.born=born
}
var fred=new employee("Fred Flintstone","Caveman",1970)
document.write(fred.constructor)
</script>
The output of the code above will be:
Êä³ö½á¹ûΪ£º
function employee(name, jobtitle, born)
{this.name = name; this.jobtitle = jobtitle; this.born = born;}
Try-It-Yourself Demos
³¢ÊÔÓëÑÝʾ
constructor - example 1
In this example we will show how to use the constructor property.
ÔÚÕâ¸ö¾ÙÀýÖÐÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓÃconstructorÊôÐÔ
constructor - example 2
In this example we will show how to use the constructor property.
ÔÚÕâ¸ö¾ÙÀýÖÐÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓÃconstructorÊôÐÔ
http://www.w3pop.com/learn/view/doc/jsref_constructor_array/
Ïà¹ØÎĵµ£º
Ò»¡¢½¨Á¢ÍøÒ³
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<script language="javascript" type="text/javascript">
<!-- Ìṩ¸øC#³ÌÐòµ÷Óõķ½·¨ -->
function messageBox(message)
{
alert(message);
}
</script>
</head>
& ......
ɾ³ýÐÐ
<script>
function del(obj)
{
obj.parentNode.parentNode.removeNode(true);
}
</script>
<body& ......
ÔÚjavascriptº¯ÊýÌåÄÚ£¬±êʶ·ûarguments¾ßÓÐÌØÊ⺬Òå¡£ËüÊǵ÷ÓöÔÏóµÄÒ»¸öÌØÊâÊôÐÔ£¬ÓÃÀ´ÒýÓÃArguments¶ÔÏó¡£Arugments¶ÔÏó¾ÍÏñÊý×飬עÒâÕâÀïÖ»ÊÇÏñ²¢²»Êǹþ¡£
javascriptº¯ÊýÌåÄÚ£¬argumentsÏñÊý×é(²¢²»ÊÇÕæµÄÊý×飬ÊÇÒ»¸öArguments¶ÔÏó£¬ÔÙ´ÎÇ¿µ÷)Ò»Ñù£¬ÓÐlengthÊôÐÔ£¬¿ÉÒÔ´ú±í´«¸øº¯ÊýµÄ²ÎÊýµÄ¸öÊý¡£
ÒýÓÃÒ»¸öÐÎʽ² ......
1. document.write( " "); Êä³öÓï¾ä
2.JSÖеÄ×¢ÊÍΪ//
3.´«Í³µÄHTMLÎĵµË³ÐòÊÇ:document- >html- >(head,body)
4.Ò»¸öä¯ÀÀÆ÷´°¿ÚÖеÄDOM˳ÐòÊÇ:window- >(navigator,screen,history,location,document)
5.µÃµ½±íµ¥ÖÐÔªËØµÄÃû³ÆºÍÖµ:document.getElementById( "±íµ¥ ......
String.replace(regexp, replaceText); ÕâÊÇStringÀàÖеÄreplace·½·¨ÔÐÍ
replace·½·¨½ÓÊÜÁ½¸ö²ÎÊý:
regexp: ÕýÔò±í´ïʽ, ÓÃÀ´ÔÚ×Ö·û´®ÖÐËÑË÷µÄ¹æÔò.
replaceText: ÓÃÀ´Ìæ»»×Ö·û´®ÖÐÆ¥ÅäÕýÔò±í´ïʽµÄ×Ó´®µÄ×Ö·û´®
ÔÚJavaScriptÖÐ, Ö§³ÖÕýÔòÌæ»», ÕýÔòÌæ»»µÄ¹æÔòÈçÏÂ:
$$: ÔÒâ´òÓ¡Ò»¸ö$·ûºÅ
$&: Óë¹æÔòÆ¥ÅäµÄÕû¸ö ......