Js-ajax Getting Started example: loops, Functions, objects, object properties

Source: Internet
Author: User
Tags define functions variable
ajax|js| Objects | functions | loops

1, enumerate the names of the object properties

<script language= "JavaScript" >
var obj=new Object ();
Obj.a= "Hello, I am Tianhongkun";
Obj.b= "What are You, Tianhongkun?" ";
Obj.c= "cc, haha, I am attribute C";
The above, of course, is to declare a new object and give the object the attribute to be assigned again
Let's do a set of objects for the above object, or add a new attribute and assign a value
Obj.d=new Object ();
Obj.d.aa= "I am the eldest of the child's object";
Obj.d.bb= "Then of course I can only do the penis, alas, ah?" Dick Halo ";
List (obj)//Call the following function to enumerate the property names of the object
Function List (obj)
{//Note that this method cannot read the predefined property names?
var name= ""; Set an empty variable
for (var i in obj)//variable I loops in obj object, this line cannot be quoted
Name +=i+ "\ n"//To call the value I read to the name variable
alert (name);//Display Name
}
</script>

2: Using the object

<script language= "JavaScript" >
var obj=new Object ();
Obj.a= "Hello, I am Tianhongkun";
Obj.b= "What are You, Tianhongkun?" ";
Obj.c= "cc, haha, I am attribute C";
The above, of course, is to declare a new object and give the object the attribute to be assigned again
Let's do a set of objects for the above object, or add a new attribute and assign a value
Obj.d=new Object ();
Obj.d.aa= "I am the eldest of the child's object";
Obj.d.bb= "Then of course I can only do the penis, alas, ah?" Dick Halo ";
Let's read it out and take a look.
Alert ("The first line is of course:" +obj.a+ "\n\t the second is" +
obj.b+ "\n\t Three is" +obj.c);
Below we bored, change a pop-up hehe, but the effect is the same
Confirm ("I am:" +obj.d.aa+ "\ n) See?" The downside is: "+OBJ.D.BB";
Note that the/n above is a newline, \ t is the alignment format displayed
</script>

3: Using a simple example of a function

<script language= "JavaScript" >
Here we define some functions as methods
function Add (x,y) {return x+y;}
Function Jian (x,y) {return x-y;}
Function Chen (x,y) {return x*y;}
function Chu (x,y) {return x/y;}
And then define a function that can take the above function as an argument.
function oper (OP1,OP2,OP3)
{//Incoming three parameters
return OP1 (OP2,OP3);//Add three parameters to the new group for multiple calls
}
var i=oper (Add, Oper (add,2,3), Oper (jian,5,4));//The result of this sentence is (2+3 + 5-4)
The above sentence seems to be more Rao, in fact, call the Oper function and give three parameters, just a set of
document.write ("Oper method results in: <b>" +i+ "<b>");/This is the truth has been a word, the back of I Bold
</script>

4; loop, judge

<script language= "JavaScript" >
for (Var i=0,fact=1,b=1;i<10;i++,fact*=i,b+=fact)
{//Affirm a i,fact,b after the assignment operation I increment 1,fact times the increment i,b add the result fact
document.write (i+ "=" +fact+ "=" +b+ "<br>");
Show i = fact = B Plus line shows the results of the next loop
}
</script>
<script language= "JavaScript" >
var name= "Mei elder sister";/if the value here equals NULL, the following will show you hello a brother Chuan
var s= "Hello a" + ((name!=null)? Name: "Chuan elder brother");/This sentence is actually a ifelse judgment statement, just use? Simplifies the
alert (s);//Bounce a dialog box out, only OK
Confirm (s);/Be sure and cancel
</script>



Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.