SoFunction
Updated on 2025-04-14

Beginners' JavaScript Chapter 1, Page 2/2


Objects in JavaScript:
The main objects in JavaScript are:
JavaScript's core objects, browser objects, user-defined objects and text objects, etc.
As an object-based programming language, JavaScript uses constructors to create object instances.
Each constructor includes an object prototype, which defines the properties and methods contained in each object. The object is dynamic and indicates the object instance.
Attributes and methods can be dynamically added, deleted or modified.

Define objects through constructor methods:
<script language="JavaScript" type="text/javascript">
<!--
//Object constructor
function Test(iName,iAddress,iGrade,iNumber)
{
=iName;
=iAddress;
=iGrade;
=iNumber;
=showInformation;
}
// Method of defining object
function showInformation()
{
statements ;
}
//Generate an instance of the object
var test = new Test('thtwin,'thtwinj2ee','th','thtwin') ;
-->
</script>

Methods for direct initialization of objects:
This method creates a custom object by directly initializing the object. The difference between the constructor method that defines the object is,
This method does not need to generate an instance of the reference object. For example:
<script language="JavaScript" type="text/javascript">
<!--
//Object constructor
//Construct nested objects
var SchoolData={
code:"0123-456-789",
Tel:"0551-1234567",
Fax:"0551-7654321"
};
//Construct the embedded object
var myTest={
name:"test",
address: "Liaoning *Huludao",
grade:"test",
number:"13400",
//Nested object SchoolData
data:SchoolData,
information:showInformation
};
// Method of defining object
function showInformation()
{
alert() ;
statements ;
}
-->
</script>
The following is the method to call the above object:
<form>
<input type="button" value="debug object" onclick="()">
</form>


Update methods or properties in objects. For example:

<script language="JavaScript" type="text/javascript">
<!--
//Object constructor
function School(iName,iAddress,iGrade,iNumber)
{
=iName;
=iAddress;
=iGrade;
=iNumber;
=showInformation;
}
// Method of defining object
function showInformation()
{
var msg="";
msg="Add new properties and new methods to the object through prototype:\n\n"
msg+="original attribute:\n";
msg+=" Organization name: "++" \n";
msg+=" Address: "+ +"\n";
msg+=" Education level: "+ +" \n";
msg+="Number of students in school: "++" \n\n";
msg+="New attribute:\n";
msg+=" Area: "++" \n";
msg+="New Method:\n";
msg+=" method returns : "++"\n";
(msg);
}
function MyMethod()
{
statements ;
}
//Generate an instance of the object
var test=new School("test","Liaoning*Huludao","test","123000");
="3000";
=MyMethod();
-->
</script>

All objects in the JavaScript language are derived from Object objects, and each object has a prototype attribute that specifies its structure.
This property describes the code and data of the object of this type. You can dynamically add new properties and new properties to the object through the object's prototype property.
method
Previous page12Read the full text