HTML5 Mobile Development Road (--javascript) Review 2

Source: Internet
Author: User

This paper is the official HTML5 training course for Brother Lian it education organization, mainly introduces: HTML5 Mobile Development Road (2)--javascript review

JavaScript Object-Oriented basics

1, how to define a class, use the following syntax to create a class

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

  1. function person (name, age) { //used to capitalize the first letter

  2. //this modified variables are called Properties

  3. this.name = name;

  4. this.age = age;

  5. //If the property value is a function, then this property is a method

  6. This.play = function () {

  7. Alert (' play football ... ');

  8. };

  9. }

2. How to create an instance of this class

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. var p = New Person (' ZS ', 22);

    2. P.play ();

    3. P.name;

3. Two other ways to create JavaScript objects

(1) First create an instance of type object and then add new properties and methods

JavaScript is a dynamic language that can add new properties and methods to objects at run time

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. var obj = new Object ();

    2. Obj.name = ' ZS ';

    3. Obj.age = 22;

    4. Obj.play = function () {

    5. Alert (' play ... ');

    6. };

(2) using the "JSON" syntax

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. var p = {' name ':' Zs ',' age ': 22};

    2. Ar p = {' name ':' Zs ',' play ':function () {

    3. Alert (' hello ');

    4. };

Or

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. var p = {name:' Zs ', Age:22,marrid:false};

    2. var p = {name:' Zs ', Play:function () {

    3. Alert (' hello ');

    4. }};

Property value if the string must be enclosed in quotation marks (single-pair)

Property value allows Number,string,boolean,null,object

[JavaScript] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. var p = {name:' Zs ',

    2. address:{

    3. City:' Beijing ',

    4. Street:' CA '

    5. }

    6. };

A complete example

[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

  1. <html>

  2. <head>

  3. <script>

  4. Function F1 () {

  5. var p = {' name ': ' Zs ', ' age ': 22};

  6. var p = {name: ' Zs ', age:22,marrid:false};

  7. var p = {name: ' Zs ',

  8. address:{

  9. City: ' Beijing ',

  10. Street: ' CA '

  11. }

  12. };

  13. alert (p.name);

  14. alert (p.address.city);

  15. }

  16. function F2 () {

  17. var arr = [

  18. {' name ': ' Zs ', ' age ': 22},

  19. {' name ': ' ls ', ' Age ': 32}

  20. ];

  21. alert (arr[1].name);

  22. }

  23. </Script>

  24. </head>

  25. <body>

  26. <input type="button" value= "clickMe" onclick="F2 ();" />

  27. </Body>

  28. </html>


HTML5 Mobile Development Road (--javascript) Review 2

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.