Object declaration methods in JS and some usages of arrays

Source: Internet
Author: User
Tags arrays

  This article describes the next JS in the object declaration and some of the use of the array, here is a good example, interested friends can refer to, I hope to help you.

  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 print 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 ();   / /Declaration properties and Behavior   stu.id=16;  stu.name= ' lamp currency ';  stu.age=function () {  return this.id; }   // Print student's information   $ (stu.id);  $ (stu.name);  $ (stu.age ());//call with parentheses     stu.sex= ' female '; Add new attribute   $ (stu.sex); Print newly added properties    //Scenario 2:  functionStudent (id,name)   {  this.id=id;  this.name=name;  this.getage=function () {  return this.id;  }   }   //use   var stu2=new Student (1, ' Yankee ');  $ (stu2.id);  $ (stu2.name);   $ (Stu2.getage ()); //Redefine a property   stu2.sex= ' pseudo Niang ';  $ (stu2.sex); //Dynamic language How to cross domain?   student.prototype.address= "Afghanistan";  $ (stu2.address);  $ ("Stu2_1 begin ...");  var stu2_1 =new Student (1, ' Young ');  $ (stu2_1.id);  $ (stu2_1.name);  $ (stu2_1.getage ());  $ (stu2_1.sex);  $ ( stu2_1.address); Cross-domain access, B-Object access to the properties of object a  //in the definition   $ ("Stu2_1 end ...");   //scheme 3:json  var stu3={id:1,name: ' Hairy heat ', Getname:function () {return this.name;}};   $ (stu3.id);  $ (stu3.name);  $ (stu3.getname ());   //var stu2   // JS inside a lot of functions and Java inside functions with the same name   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 ());     /talk again array   var arr1=new array (3);  arr1[0]=10;  arr1[1]=20;  arr1[2]=3;  _ (ARR1); nbsp arr1[3]=4; //  _ (arr1);   //array 2  var arr2=new array (234,345,2354,2134,234);  _ (ARR2); nbsp  //array 3  var arr3=new array ();  arr3[0]=10;  arr3[1]=20;  arr3[2]=3;  _ (ARR3);   //Array 4. Recommended notation   var arr4=[];  arr4[0]=10;  arr4[1]=20;  arr4[2]=3;  _ (ARR4);   / /array 5. Recommended notation   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));//number of parameters automatic identification     VAR Pers On=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 (1, "Chen Xin");  $ (p.id);   $ (p.name);  $ (p.t ());/normal access   $ (p.test ()); No access   </script>  </head>  <body>  </body>  </html>     
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.