JavaScript Handout (ii)

Source: Internet
Author: User
Tags map data structure

Statement:
-----------------------------------
Basic execution Unit, with all statements ending with semicolons
The With statement avoids repeated input, such as
With (document)
{
Writeln ("a");
Writeln ("B");
Writeln ("C");
}

Process Control:
-----------------------------------
Branch:
Cycle
For,while,do while
For in: Essentially a foreach loop that iterates through the array elements and iterates through all the properties of the JS object. Like what:
For (propname in navigator)
{
document.write (propname+ ': ' +navigator[propname]);

Function:
-----------------------------------
A class citizen, the function itself is an object, which can be used entirely as a category.
Defining named Functions
To define an anonymous function:
function (Poarameter list)
{
};
This function definition does not need to specify a function name, and the parameter list is immediately followed by the function keyword. Last followed by a semicolon
Use function class anonymous functions:
This class can be used to define functions, such as
var f = new Function (' name ', "Document.writeln (' adaf<br> ');");
The above code defines an anonymous function with the new function (), assigns the anonymous function to the F variable, and accesses the anonymous function through F. The last string represents the function execution body.

Local function: External unreachable

Instance properties and Class properties of functions: functions are not only functions, but also classes, which are the only constructors of this class. There are 3 types of variables in the function:
Local variables: General method declaration
Instance properties: A variable that is decorated with the this prefix in a function.
Class Property: A variable that is decorated with a function name prefix in the functions.
Instance properties (this prefix decoration) must be accessed through the object, and class properties (the function name prefixes) must be accessed through the class (that is, the functions). Like what
function person (national,age)
{
This.age=age;
person.national=national;
var bb=0;
}
JS is not like Java, it is a dynamic language that allows you to add properties and methods to objects at any time. When we assign a value directly to a property of an object, we can consider adding properties to the object.

3 Ways to call a function:
Direct invocation: such as Window.alert ("Sdfafs");
Called With Call (): Its syntax format is a function reference. Call (caller, arg1,arg2 ...).
with apply () Call: The difference from call () when calling a function by using calls (), each parameter must be listed in detail, and apply () can represent all parameters in parentheses with arguments.
Like what
Myfun.call (window,12,23);
Myfun.apply (this,arguments);

Function independence: Because it is a class citizen, it does not belong to any objects and classes.


parameter handling of functions
-----------------------------------------------------------
The parameters of the JS function are all passed by value, and when the function is called through an argument, it is not the actual argument itself, but the copy of the argument.
For composite type parameters, it is still true that value is passed, but it is easy to confuse.
function Changeage (person)
{
person.age=10;
Person=null;
}
var person={age:5};
Changeage (person);
The key above is that the variable of the composite type does not itself hold the object itself, and the variable of the compound type is simply a reference to the actual JavaScript object. When a variable of the person compound type is passed into Changeage (), the passed-in is still a copy of the person variable, except that the copy and the original person variable point to the same JS object.

Null parameter: There is no program problem, only the parameter value of the incoming argument will be treated as undefined.

Parameter type: The argument list does not require a type declaration, which is a hidden hazard for function calls. Like what:
function Changeage (p)
{
P.setage (34);
}
The incoming p can be a fully shaped/Boolean variable, and none of these types of data have the Setage () method, but the program forces the method to be called, which definitely causes the program to fail. So JS than Java,c language program robust.
In order to solve this problem of weakly typed language, Duck type is presented. First of all to determine whether the entry object can make a quack sound, and has the characteristics of walking left and right sway, the program can be used as a duck. If a function of a weakly typed language needs to accept a parameter, you should first determine the parameter type and determine if the parameter contains the property or method you want to access.

Working with objects
----------------------------------------------------
For better software reuse, we recommend the use of objects and functions
JS does not allow you to specify an inheritance relationship between a class and a class, all classes are subclasses of object.

objects and associative arrays: JS objects differ from pure object-oriented language objects, and the JS object is essentially an associative array, or more like a map data structure in Java, consisting of a set of key-value pairs. Unlike the map object in Java, the value of a JS object is not only a value but also a function, at which point the function is the method of the object, and when Vvalue is a value of the base type or compound type, value at this time is the property value of the object. When you need access to an object's properties, you can not only obj.propname it, you can also obj[propname it, and sometimes you have to use this form. Like what
For (propname in P)
{
Document.writeln (propname+ ":" +p[propname]);
}
Each property of a person object is traversed because the loop counter is the property name of the person object when traversing each property, so the program must be accessed according to the property name. You cannot use P.propname at this time, because JS does not treat propname as a variable, it tries to directly access the PropName property of the object, but the property does not actually exist.

Creating objects
----------------------------------------------------
The JS object is just a special associative array. There are 3 ways of creating objects:
Call the constructor with new, such as Var p1=new person ();
Create objects directly with object, such as Var myobj=new object (); Myobj.name= ' SFSF ';
To create an object with JSON syntax:
JSON has evolved into a lightweight, cross-language data interchange format. You can avoid writing functions with JSON syntax, or you can create a JS object directly by using new.
For example var p={"name": ' Sdfsfs ', "gender": ' SDFSDF '
};

JavaScript Handout (ii)

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.