JavaScript basic syntax

Source: Internet
Author: User

1. Variables

JavaScript is a weakly typed language, there is no type variable, that is, a variable can store any type of data

Define variables to be defined with Var

Example: Var x=3; var x= "string";

Note: Both single and double quotes in JavaScript can define a string, undefine is a constant value that is not initialized by a variable in JS

2. Statements

Roughly the same as Java

Note: null is false in JavaScript, non-null is True

3. Functions

JavaScript does not have a type, the function does not involve return type and parameter type

Use function to define functions

Called in <script></script> when the function is called

Note:<script></script> is the code range for JavaScript

Example: General functions

<script>    function  Show (x, y)    {    }</script>

Dynamic functions: That is, parameters and function bodies can be passed in as variables.

var New Function("x", "Y"; " Alert (x+ "" +y); ");

Anonymous functions: Can be used as a way to handle an event

var function (){}

Note: A function can also have a return value, and if a function without () is assigned a value, it is the processing of the assigned function, and if so, the return value of the assigned function.

You can use functions to create objects, object-oriented thinking, code examples:

<script>    function person (name, age)    {        this. Name = name;          this. Age = Age ;    }     var New Person ("Lisi");     = "Boy"    function()    {        alert ("Walk");    } </script>

Note: There are two ways to invoke the top object's own property, which can be called through., or by p["property name", what is the difference between the two?

P.name This method is only suitable for a single call, p["name"] this way can implement bulk call

For example, p has more than one property, the property name of P is read out in an array m, when the bulk call can be used p["m[i]" this way to achieve

    

JavaScript basic Syntax

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.