Object declaration methods and array usage in js
This article introduces the object declaration method in js and some usage of arrays. The following is a good example. If you are interested, please refer to it and hope to help you.
The Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> New Document </title>
<Meta name = "Generator" content = "EditPlus">
<Meta name = "Author" content = "">
<Meta name = "Keywords" content = "">
<Meta name = "Description" content = "">
<Script>
// Define a print function
Var $ = function (str ){
Document. write (str );
Document. write ("<br/> ");
}
// Define the print Array Function
Var _ = function (arr ){
For (var tmp in arr)
{
$ (Arr [tmp]);
}
}
// Define a student object
Var stu = new Object ();
// Declare attributes and Behaviors
Stu. id = 16;
Stu. name = 'lamp coin ';
Stu. age = function (){
Return this. id;
}
// Print Student Information
$ (Stu. id );
$ (Stu. name );
$ (Stu. age (); // brackets are required for calling.
Stu. sex = 'female '; // Add a new property
$ (Stu. sex); // print the newly added property
// Solution 2:
Function Student (id, name)
{
This. id = id;
This. name = name;
This. getAge = function (){
Return this. id;
}
}
// Use
Var stu2 = new Student (1, 'yangdun ');
$ (Stu2.id );
$ (Stu2.name );
$ (Stu2.getAge ());
// Define another attribute
Stu2.sex = 'pseudoniang ';
$ (Stu2.sex );
// How does the Dynamic Language perform cross-origin?
Student. prototype. address = "Afghanistan ";
$ (Stu2.address );
$("Stu2_1 begin ...");
Var stu2_1 = new Student (1, 'yangdun ');
$ (Stu2_1.id );
$ (Stu2_1.name );
$ (Stu2_1.getAge ());
$ (Stu2_1.sex );
$ (Stu2_1.address); // cross-origin access. object B accesses the attributes of object.
// In the definition
$("Stu2_1 end ...");
// Solution 3: json
Var stu3 = {id: 1, name: 'mao Yanyan ', getName: function () {return this. name ;}};
$ (Stu3.id );
$ (Stu3.name );
$ (Stu3.getName ());
// Var stu2
// Many functions in js have the same name as those in java.
Var str1 = new String ("abcd ")
Var str2 = "asdf ";
$ (Str1.indexOf ('C '));
$ (Str1.charAt (3 ));
$ (Str2.charAt (3 ));
$ ("Absdf". substring (2, 4 ));
Var day = new Date ();
$ (Day. getYear ());
$ (Day. toLocaleString ());
// Review the Array
Var arr1 = new Array (3 );
Arr1 [0] = 10;
Arr1 [1] = 20;
Arr1 [2] = 3;
_ (Arr1 );
Arr1 [3] = 4;
//
_ (Arr1 );
// Array 2
Var arr2 = new Array (234,345,235, 234 );
_ (Arr2 );
// Array 3
Var arr3 = new Array ();
Arr3 [0] = 10;
Arr3 [1] = 20;
Arr3 [2] = 3;
_ (Arr3 );
// Array 4. Recommended syntax
Var arr4 = [];
Arr4 [0] = 10;
Arr4 [1] = 20;
Arr4 [2] = 3;
_ (Arr4 );
// Array 5. Recommended syntax
Var arr5 = [3254,43, 532,45, 2345];
_ (Arr5 );
Function add (I, j ){
Return I + j;
}
Function add (I, j, k ){
Return I + j + k;
}
$ (Add (1, 2); // automatically identifies the number of parameters
Var Person = function (id, name)
{
This. id = id; // public
This. name = name; // public
Var I = 1; // private
Function test () {// private
Alert ('asdf ');
}
This. t = function () // public
{
Return 1;
}
}
Var p = new Person (1, "Chen Xin ");
$ (P. id );
$ (P. name );
$ (P. t (); // normal access
$ (P. test (); // cannot be accessed
</Script>
</Head>
<Body>
</Body>
</Html>