📄 js_objects.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Create Your Own Objects</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript Create Your Own Objects</h1><a href="js_timing.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_summary.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Objects are useful to organize information.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_create_object1">Create a
direct instance of an object</a></p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_create_object2">Create a
template for an object</a></p>
<hr />
<h2>JavaScript Objects</h2>
<p>Earlier in this tutorial we have seen that JavaScript has several built-in objects, like String, Date, Array, and more. In addition to these built-in objects, you can also create your
own.</p>
<p>An object is just a special kind of data, with a collection of properties and methods.</p>
<p>Let's illustrate with an example: A person is an object. Properties are the values associated with
the object. The persons'
properties include name, height, weight, age, skin tone, eye color, etc. All persons
have these properties, but the values of those properties will differ from
person to person. Objects also have methods. Methods are the actions that can be performed on
objects. The persons' methods could be eat(), sleep(), work(), play(), etc.</p>
<h3>Properties</h3>
<p>The syntax for accessing a property of an object is:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table10">
<tr>
<td valign="top">
<pre>objName.propName</pre>
</td>
</tr>
</table>
<p>You can add properties to an object by simply giving it a value. Assume that
the personObj already exists - you can give it properties named firstname,
lastname, age, and eyecolor as follows:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table11">
<tr>
<td valign="top">
<pre>personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=30;
personObj.eyecolor="blue";</pre>
<pre>document.write(personObj.firstname);</pre>
</td>
</tr>
</table>
<p>The code above will generate the following output:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table13">
<tr>
<td valign="top">
<pre>John</pre>
</td>
</tr>
</table>
<h3>Methods</h3>
<p>An object can also contain methods.</p>
<p>You can call a method with the following syntax:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table15">
<tr>
<td valign="top">
<pre>objName.methodName()</pre>
</td>
</tr>
</table>
<p><b>Note:</b> Parameters required for the method can be passed between the
parentheses.</p>
<p>To call a method called sleep() for the personObj:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table18">
<tr>
<td valign="top">
<pre>personObj.sleep();</pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>Creating Your Own Objects</h2>
<p>There are different ways to create a new object:</p>
<p><b>1. Create a direct instance of an object</b></p>
<p>The following code creates an instance of an object and adds four properties
to it:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table19">
<tr>
<td valign="top">
<pre>personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";</pre>
</td>
</tr>
</table>
<p>Adding a method to the personObj is also simple. The following code adds a
method called eat() to the personObj:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table20">
<tr>
<td valign="top">
<pre>personObj.eat=eat;</pre>
</td>
</tr>
</table>
<p><b>2. Create a template of an object</b></p>
<p>The template defines the structure of an object:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table23">
<tr>
<td valign="top">
<pre>function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}</pre>
</td>
</tr>
</table>
<p>Notice that the template is just a function. Inside the function you need to
assign things to this.propertyName. The reason for all the "this" stuff is that you're going
to have more than one person at a time (which person you're dealing with must be
clear). That's what "this" is: the instance of the object at hand.</p>
<p>Once you have the template, you can create new instances of the object, like
this:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table24">
<tr>
<td valign="top">
<pre>myFather=new person("John","Doe",50,"blue");
myMother=new person("Sally","Rally",48,"green");</pre>
</td>
</tr>
</table>
<p>You can also add some methods to the person
object. This is also done inside the template:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table25">
<tr>
<td valign="top">
<pre>function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;</pre>
<pre>this.newlastname=newlastname;
}</pre>
</td>
</tr>
</table>
<p>Note that methods are just functions attached to objects. Then we will have to write the newlastname() function:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3" id="table8">
<tr>
<td valign="top">
<pre>function newlastname(new_lastname)
{
this.lastname=new_lastname;
}</pre>
</td>
</tr>
</table>
<p>The newlastname() function defines the person's new last name and assigns that to
the person. JavaScript knows which person you're
talking about by using "this.". So, now you can write:
myMother.newlastname("Doe").</p>
<hr />
<a href="js_timing.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_summary.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -