Copy the code code 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 printing function
var $=function(str){
document.write(str);
document.write("<br/>");
}
//Define print array function
var _=function(arr){
for(var tmp in arr)
{
$(arr[tmp]);
}
}
//Define a student object
var stu=new Object();
//Declare properties and behaviors
stu.id=16;
stu.name='Lamp Coin';
stu.age=function(){
return this.id;
}
//Print student information
$(stu.id);
$(stu.name);
$(stu.age());//Add parentheses when calling
stu.sex='female'; //Add new attributes
$(stu.sex); //Print the newly added attributes
//Option 2:
function Student(id,name)
{
this.id=id;
this.name=name;
this.getAge=function(){
return this.id;
}
}
//use
var stu2=new Student(1,'Yangton');
$(stu2.id);
$(stu2.name);
$(stu2.getAge());
//Define another attribute
stu2.sex='transvestite';
$(stu2.sex);
//How does dynamic language cross domains?
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-domain access, b object accesses the attributes of a object
//In definition
$("stu2_1 end...");
//Option 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 functions 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());
//Let’s talk about arrays again
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,2354,2134,234);
_(arr2);
//Array 3
var arr3=new Array();
arr3[0]=10;
arr3[1]=20;
arr3[2]=3;
_(arr3);
//Array 4. Recommended writing method
var arr4=[];
arr4[0]=10;
arr4[1]=20;
arr4[2]=3;
_(arr4);
//Array 5. Recommended writing method
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));//Automatic recognition of 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);
$(pt());//Normal access
$(p.test()); //Cannot access
</script>
</head>
<body>
</body>
</html>