JavaScript-learning notes for Object-based scripting language (I), javascript scripting language

Source: Internet
Author: User

JavaScript-learning notes for Object-based scripting language (I), javascript scripting language

1. Two js embedding Methods
Use a javascript prefix to construct a url: <a href = "javascript: alert ('run JavaScript .. ') "> RUN js </a>
Place js scripts between <style> </style>:

<Style type = "text/javascript"> alert ("run JavaScript .. ") </Script>
2. If no declared variables are used directly, the following error occurs: ReferenceError: x is not defined.

If no value is declared, the variable value is undefined.

3. Save the js file separately
   <script src="test.js" type="text/javascript"></script>

4. Three forced type conversion functions
ToString () converts Boolean and numeric values to strings
ParseInt () converts string and boolean values to Integers
ParseFloat () converts string and boolean values to floating point numbers.
5. javascript variables have no block range, which is different from Java and C ++.
6. If var is used to define a variable, the program will force a new variable to be defined.
Although Javascript is a weak scripting language, you do not need to specify the basic type of the variable when declaring the variable. But the js Value
There are also types of data stored in the memory. There are five basic types.
7. When Infinity and Infinity perform arithmetic operations with other values, the entire formula is: NaN
The two Infinitiy values are equal.
Two NaN unequal
8. composite types in js
Object: Object
Array: Array
Function: Function
9. The array element types in js are not required to be the same, and the array length can be changed at will.
When accessing an array element, it does not generate an array out of bounds. It only displays the value of this element as undefined.
10. void forcibly specifies that the expression does not return values.
11,
Var a = []; for (var I = 0; I <10; a [I ++] = I + 10); // use an empty statement for (index in) {document. writeln (a [index] + "<br/>");} // index stores the index value.

12. js supports self-defined errors. js has only one exception class: Error, and catch block classes do not need to declare exception classes.
For (var I = 0; I <10; I ++) {if (I> 4) throw new Error ("user-defined Error"); document. writeln (I + "<br/> ");}
13. Use with to avoid repeated object writing
    with(object){statements}
For example, the province has written a document
with(document){ writeln( "hello<br/>"); writeln( "hello2<br/>");  writeln( "hello3<br/>");}
14. js functions are reusable "blocks" and are also a Function instance.
Anonymous functions are recommended.
Var f = function (parameter) {function body };
Example:
Var a = function () {alert ("call anonymous function")}; a (); // function call

15. recursion must be traced back to known points.

★16. Note: js functions are not only a function, but also a class. When defining js functions, a class with the same name as this function is also obtained.
This function is also the unique constructor of this class. (When defining a function, a constructor is also defined. when defining a function,
The variable modified with this in the function is an instance property. If a property value is a function, it can be considered as a method)
Therefore, there are two ways to call a function:
1. Direct call
2. call with the new Keyword, so there will always be a return value, and the return value is a js object.
Example: function Person (name, age) {this. age = age; this. name = name; this.info = function () {document. writeln ("My name is:" + this. name + "</br>"); document. writeln ("My age is:" + this. age + "</br>") ;};}// create the p object var p = new Person ("Zhou pengcheng", 24); p.info (); // The Person class defines a method to make the code more concise by using anonymous Functions
Attaches a function defined in js to an object as a method for this object.
In fact, if you do not explicitly specify the object to which the function is "appended", the function will be "appended" to the window object.

17. js is different from Java. It is a dynamic language that allows adding attributes and methods to objects at any time.
In fact, when we directly assign values to an attribute of an object, it is regarded as adding an attribute to the object.
Note: JavaScript objects cannot access the class attributes of their classes.
If a global variable is defined directly, the variable will be "appended" to the window object and used as the instance attribute of the window object.

18. Three Methods for calling functions in js
1. Direct call: use the "APPEND" Object of the function as the caller, and input parameters in the brackets of the function to call the function.
2. call () method dynamic call Function
Example:
Var each = function (arr, fn) {for (var s in arr) {fn. call (null, s, arr [s]) ;}} each ([4, 20, 3], function (a, B) {document. writeln ("the element of the array" + a + "is:" + B + "<br/>") ;}); // caller. function (parameter 1, parameter 2 ...) <=> function. call (caller, parameter 1, parameter 2 ...)

3. Call a function using the apply () method
When calling a function through call (), each parameter must be listed in detail in parentheses.
When using apply () to call a function, arguments can be used in parentheses to represent all parameters.
Var myfun = function (a, B) {alert ("a =" + a + "\ nb =" + B);} myfun (1, 2); myfun. call (window, 1, 2); var fun2 = function (num1, num2) {myfun. apply (this, arguments);} fun2 (1, 2); myfun. apply (window, [1, 2]); // arguments represents all parameters. arguments is essentially an array.

19. In js, a function is a "first-class citizen", which is always independent and never belongs to other classes or objects.
Function Person (name) {this. name = name; this.info = function () {alert ("My name is:" + this. name) ;}} var per = new Person ("zpc"); per.info (); var name = "window name"; per.info. call (window); // p.info () can reference this function. You can use the call () method to specify the caller.

20. Empty parameters can be used in js. The parameter values that are not passed in will be processed as undefined. In js, function names are unique identifiers of functions.
If the function name is the same, it will only overwrite
Function test1 () {alert ("test1");} function test1 (s) {alert ("test1" + s) ;}test1 (); // output test1undefined

21. Because Javascript is a weak type language, the list of function-defined parameters does not require type declaration, which poses a hidden risk for function calls.
The theory of "duck type" can be used to improve this problem: in the function, first judge the parameter type and determine whether the parameter contains the attribute and method to be accessed,
When these conditions are met, the program starts to actually process the properties and methods of the called parameters.

22. The essence of js objects is to associate arrays.
Example:
Function Person (name, age) {this. age = age; this. name = name; this.info = function () {alert ("info method! ") ;}} Var p = new Person (" zpc ", 24); // alert (p [age]); alert (p ['age']); // The correct age alert (p. age); for (proname in p) {document. writeln ("p object" + proname + "attribute value:" + p [proname] + "<br/> ");}

23. Avoid using embedded functions as the class definition method whenever possible. Instead, add methods by adding the prototype attribute. Use the prototype attribute to dynamically
Adding a method will make the program safer and the performance more stable. All classes (that is, functions) in JavaScript have a prototype attribute.
When adding a function or attribute to a property, it can be considered as an extension of the original class. You can use the prototype attribute to extend the built-in js classes.
Example:
Function Person (age, name) {this. name = name; this. age = age; this.info = function () {document. writeln ("name:" + this. name + "</br>"); document. writeln ("Age:" + this. age + "</br>") ;}} var p1 = new Person (23, "Zhou pengcheng"); p1.info (); // p1.walk (); there is no way to add the walk Method to Person. // All Person instances share a walk method, saving the memory of Person. prototype. walk = function () {document. writeln (this. name + "is slowly slipping .. <Br/> ");} document. writeln ("<br/>"); var p2 = new Person (25, "bird peng"); p2.info (); p2.walk (); p1.walk ();

24. Three methods for creating objects in js:
1. Use the new Keyword to call the constructor to create an object, as shown in the preceding example.
2. directly create an Object using an Object
Var per = new Object (); per. name = "bird peng"; per. age = 24; per ["gender"] = "male"; // js allows dynamic addition of attributes. The essence of js objects is to associate the array document. writeln (per. name + "<br/>" + per. age + "<br/>" + per. gender );
Js allows you to add an existing function as an object.
Example:
Var per = new Object (); per. name = "bird peng"; per. age = 24; function abc () {document. writeln ("Object name attribute:" + this. name + "<br/> age attribute of the object:" + this. age + "<br/>") ;}per.info = abc; // Add the method per.info () where the abc function is per object ();

Js defines anonymous functions using the new Function () method. You can add methods for js objects in this way.
Example:
Var per = new Object (); per. name = "bird peng"; per. age = 24; per.info = new Function ("document. writeln ('object name: '+ this. name); '<br/>'; document. writeln ('object age attributes: <span style = "font-family: Arial, Helvetica, sans-serif;"> '+ this. age); "); </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> per.info (); </span>
3. Create an object using json syntax
Var p = {name: "zpc", age: 32}; alert (p); // The above is the p object var person = {name: "Zhou pengcheng", gender: 'male', // The attribute value can be any basic type, or even the object son: {name: "peng", age: 12}, info: function () {document. writeln ("name:" + this. name); document. writeln ("gender:" + this. gender); document. writeln ("son:" + this. son) ;}} person.info (); // The above is the person object created using the json syntax. In fact, the json syntax is not only used to create objects, it is also very common to create Arrays Using JSON syntax. The traditional method of creating arrays is var a = new Array (); a [0] = 'zpc '; a [1] = 'niao'; traditional Array Creation Method 2: var a = new Array ('zpc', 'niao'); JSON Array creation: var a = ['zpc', 'niao']; // a complex JSON object var person = {name: "Zhou pengcheng", age: 24, // The third attribute is the array schools: ['Primary ', 'secondary', ''], // The fourth attribute is the object array parents: [{name: 'Father ', age: 50, address: 'shanghai'}, {name: 'mother', age: 49, address: 'nanjing '}]}; alert (person. parents); alert (person. parents [0]. name); // In fact, json has developed into a lightweight, cross-language data exchange format, becoming a strong competitor of xml data exchange formats.














































































































































JavaScript is a JAVA script language version.

They are completely different.
The differences between the two are as follows:
First, they are two different products developed by two companies. Java is a new generation of object-oriented programming language launched by SUN, which is particularly suitable for Internet application development; javaScript is a product of Netscape. It is designed to expand the Netscape Navigator function. It is an object-and event-driven explanatory language that can be embedded into Web pages.

Secondly, JavaScript is object-based, while Java is object-oriented, that is, Java is a real object-oriented language. Even if you develop simple programs, you must design objects. JavaScript is a scripting language that can be used to create complex software that is unrelated to the network and interacts with users. It is an object-and event-driven programming language. Therefore, it provides a wide range of internal objects for designers.

Third, the two languages are executed differently in their browsers. Java source code must be compiled before it is passed to the client for execution. Therefore, the client must have a simulator or interpreter on the corresponding platform, it can use compilers or interpreters to compile code independently from a specific platform. JavaScript is an interpreted programming language. Its source code does not need to be compiled before it is sent to the client for execution. Instead, it sends the character code in the text format to the client and is interpreted and executed by the browser.

Fourth, the variables used by the two languages are different. Java uses a strongly typed variable check, that is, all variables must be declared before compilation. The variable declaration in JavaScript uses its weak type. That is, variables do not need to be declared before use, but the interpreter checks their data types at runtime.

Fifth, the code format is different. Java is an HTML-independent format. It must be loaded by referencing external media in HTML, and its code is saved as byte code in an independent document. JavaScript code is a text character format that can be directly embedded into HTML documents and loaded dynamically. Compiling HTML documents is as convenient as editing text files.

Sixth, the embedding method is different. In HTML documents, the two programming languages have different identities. JavaScript uses <script>... </script>, while Java uses <applet>... </applet>.

7. Static binding and dynamic binding. Java uses static concatenation, that is, Java object reference must be carried out during compilation, so that the compiler can implement forced type check. JavaScript uses dynamic concatenation, that is, JavaScript Object Reference is checked at runtime. If it is not compiled, the object reference check cannot be implemented.

JavaScript is a client scripting language driven by objects and events with relative security.

Fun1: display the number value + 1 in the input box.
Fun2: if the value of the input box is smaller than-1, 0 is displayed in the input box; otherwise,-1 is displayed.

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.