"JavaScript core concepts and practices"--Reading notes

Source: Internet
Author: User

Objective:

the programming language is mainly divided into two kinds of 1, based on the storage principle (von * Neumann system) of the command-type programming language, such as C + +, Java, Object Pascal (DELPHI).
2. A functional programming language, such as Lisp, Scheme, produced by the lambda calculus of the Chu Qi of the Aron left *.
The era of non-interference between the two languages (JavaScript appeared before), program = data structure + algorithm
JavaScript, known as the C-Grammar Lisp, perfectly combines these two systems (C's syntax, Lisp's kernel).

This book is divided into two parts 1, the core concept of JavaScript language: JavaScript objects, functions, arrays, closures
2, JavaScript support programming Paradigm: JavaScript engine, JavaScript in Java, C, C + + and other applications in the use of server-side JavaScript applications.



Chapter I. Overview

A brief History of 1.1 JavaScript
Mocha (1996)--LiveScript--JavaScript--ECMAScript
JSP, ASP can provide dynamic content for the page, but without JavaScript will not be able to dynamically modify the page in front of the server after the return, there is no page effects.

1.2 JavaScript language features
dynamic, weak-type, prototype-based scripting language.
The complete "Everything is Object": even if the function is the object of the code itself, the boundary between the data and the code is quite vague.

1.2.1 Dynamic nature:

1. Assign a value to a property without having to create a field in advance, as long as it is directly assigned when used              

1 var new object (); // defining an Object 2 obj.name= "an object"; // Create properties dynamically name 3 function () {return "Hi";}; // dynamically creating Properties Sayhi 4 5 Delete obj.name; // delete an object's properties

In a static language, you need to pre-define the properties of the object and the type of the property itself. Once the definition is complete, the structure of the object is finalized, and the structure cannot be changed. Usually the object inherits some methods that it cannot use, and cannot be deleted.

2. Dynamic access to properties of a JavaScript object

1 var key = "Property"; 2 print (key); 3 4  var obj = {property: "MyProperty"}5 print (Obj[key]);

This feature can make the code more concise and clear, such as the ability to dynamically generate the object's property name from the code, and then remove the property values.

 

1.2.2 Weak type: The data type does not need to be specified at the time of Declaration, and the interpreter instantiates the variable based on the context.
              Weak types also have disadvantages, such as the development of object-oriented JavaScript, where there is no type of judgment, but can be resolved by other means.

1.2.3 Object-oriented: Everything in JavaScript is an object, even a function that is used to send logic to the table. The code itself is also an object, such as the code itself can be passed as a parameter to other code                   

 1  var  array = [1,2,3,4,5 2  array.map (function  (item) {return  item*2 3  var  staff = [{name: ' Lilei ', age:25},{name: ' Hanmeimei ', Age:24},{name: ' Hobo ', age:25  5  staff.map (function  (item) {return   item.name.toUpperCase});  6  staff.filter (function  () {Returen ITEM.AGE>24});  


1.2.4 Interpretation and compilation: JavaScript is an interpreted language in the browser
in Rhino (Java version of JavaScript interpreter), JavaScript-compiled (can be compiled into Java bytecode)
in the Google V8 engine, JavaScript code is compiled directly into native code without explanation.

Explanation: You can modify the code at any time, without compiling, refresh the page to re-explain
inferior: Because each time needs to explain, the program cost is big.
compile-good: only need to compile once, run the compiled code each time
inferior: Loss of dynamic

1.3 JavaScript Application scope


Most of the current JavaScript runs on the client.
Partially run on server side, such as servlet, ASP, etc.
Run as a standalone language in other applications, such as Rhino (Java), SpiderMonkey (C)

1.3.1 Client JavaScript
With the revival of Ajax, client-side JavaScript has evolved, with a large number of JavaScript libraries, such as jquery, ExtJS, Backbone.js, MooTools, and so on.

1.3.2 Server-side JavaScript
Node.js,helma,apache sling and so on.

Examples of jaxer:

1<script>2JQuery (function(){3$ ("form"). Submit (function(){4Save ($ ("textarea"). Val ());5 return false;6 });7 });8</script>9<script runat= "Server" >Ten functionSave (text) One { AJaxer.File.write ("Temp.txt", text); - } -Save.proxy =true; the   - functionload () - { -$ ("textarea"). Val ( +Jaxer.File.exists ("Tmp.txt")? Jaxer.File.read ("Tmp.txt"): "" - ); + } A</script> at<script> - varrs = Jaxer.DB.execute ("Select * FROM table"); - varfield = Rs.rows[0].field; -</script>

node. JS: node. JS runs a module specification Comminjs that implements JavaScript, making it possible for large-scale JavaScript development, one module to use any other module, or to export the functions that it needs to expose to other modules.

<script>varSYS = require (' sys ')); HTTP= Require (' http '); Http.createserver (function() {setTimeout (function() {Res.sendheader (200,{' content-type ': ' Text/plain '}); Res.sendbody (' Hello World '); Res.finish ();},2000)}). Listen (8000); Sys.puts (' Server running at Http://127.0.0.1:8000/');</script><script>varTCP = require (' TCP ');varServer = Tcp.createserver (function(socket) {socket.setencoding ("UTF8"); Socket.addlistener ("Connect",function() {Socket.send ("Hello\r\n");}); Socket.addlistener ("Receive",function(data) {socket.send (data);}); Socket.addlistener ("EOF",function() {Socket.send ("Goodbye\r\n"); socket.close ();} );</script> 



Chapter II Basic Concepts

2.1 Data Types
data types in JavaScript are divided into 1, basic data types: implicitly converted to objects when necessary
2. Object type: Object, array, function

2.1.1 Data type: string, number, Boolean, Undefined, null, object (6 kinds)

2.1.2 Object Type: The object mentioned here is not the object itself, but refers to a type.
includes an object (a collection of properties, a hash of key values), an array (ordered list), a function (containing executable code).
An object type is a composite data type whose basic elements consist of a basic data type, but not a friend base type, such as a value in an object type can make other object type instances.

2.1.3 The transformation between the basic type and the object
    
the base type is converted to an object:
basic data types when doing some arithmetic, an object is temporarily wrapped, and after the operation is finished, the object is automatically released.

the object is converted to the base type:
The value of the object is obtained by invoking the valueof () method of the object, which is used if it matches the type of the context. If valueof () does not take a value, you need to call the object's ToString () method, and if the context is numeric, then you need to convert the string to a numeric value. ValueOf () has a higher priority than ToString ().
in fact, this conversion rule can cause many problems, such as that all non-empty objects will be converted to true in a Boolean environment
if (Datamodel.item) {...} else{...}
Datamodel.item simplifies the notation of if (datamodel.item! = null) when converting to a Boolean value while judging whether it is empty

2.1.4 Type of judgment

Example: When a function parameter is a function, the type of the parameter needs to be judged, combined with the typeof operator and the instanceof operator, to determine

1 functionhandlemessage (Message,handle)2 {3     returnhandle (message);4 }5 6 //determine if handle is a function before calling7 functionhandlemessage (Message,handle)8 {9     if(typeofHandle = = "function")Ten{returnhandle (message);} One     Else A{Throw NewError ("The 2nd argument should be a function");} -}

When the typeof phase is simultaneous:

1 varobj = {};2 varArray = ["One", "one", "three"];3 4PrinttypeofMB ();//Object5PrinttypeofArray);//Object6 7Print (objinstanceofArray);//false8Print (arrayinstanceofArray);//true

2.2 Variables

A variable is a reference to the storage space of a value, and a value is associated with a name, which can be referenced later by a variable.

2.2.1 Basic types and reference types

The base type has a fixed size in memory.

The reference type is in principle no size limit, and we access it by

  

"JavaScript core concepts and practices"--Reading notes

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.