Get familiar with JavaScript

来源:互联网 发布:os x 软件推荐 程序员 编辑:程序博客网 时间:2024/06/14 13:18

The core JavaScript language definies a minimal API for working with text, arrays, dates, and regular expressions but does not include any input or output functionality.
Input and output (as well as more sophisticated features, such as networking, storage, and graphics) are the responsibility of the "host environment" withiin which JavaScript is embeded.
Usually that host envrionment is a web browser. Browser-based APIs loosely known as "client-side JavaScript".

JavaScript离不开库的支持,这也就是为什么JQuerry库和JS如此紧密绑定。当然你也可以选用别的库,但是无可否认JQurry是最火的。

 

JavaScript is an object-oriented language. but it is quite different than most.

JavaScript的面向对象特性需要用闭包的概念来模拟,这个稍后会有专门文章进行介绍。

 

The JavaScript interpreter performs automatic garbage collection for memory management. This means that a program can create objects as needed, and the programmer never needs to worrry about destruction or deallocation fo the those objects.

 

Variables are declared with the "var" keyword. JavaScript variables are untyped: you can assign a value of any type to a variable, and you can later assign a value of a different type to the same variable.

 

JavaScript does not have "block scope". It uses "functuion scope".
function scope: variables are visible within the function in which they are defined and within any functions that are nested within that function. Curiously, this means that variables are even visible before they are declared. Although the local variable is defined throughout, it is not actually initialized until the var statement is executed.

 

JavaScript的简单类型包括数字,字符串,布尔值,null值和undefined值。其他所有的值都是对象。数字,字符串和布尔值“貌似”对象,因为它们拥有方法,但它们是不可变的。JavaScript中的对象是可变的键控集合(keyed collections)。在JavaScript中,数组是对象、函数是对象,正则表达式是对象,当然,对象自然也是对象。
Objects:
object.property       -->(property) identifier which is static and must be hardcoded in the program
object["property"]    -->("property") the name of the property is expressed as a string, which can be manipulated and created while a program is running.
对象是属性的容器,其中每个属性都拥有名字和值。属性的名字可以是包括空字符串在内的任意字符串。属性值可以是除undefined值之外的任何值。

Suppose you assign to the property x of the object o. If o already has an own(noninherited) property named x, then the assignment simply changes the value of this existing property. Otherwise, the assignment creates a new property named x on the object o. If o previously inherithed the property x, that inherited property is now hidden by the newly created own property with the same name.

The inheritance occurs when querying properties but not when setting them is a key feature of JavaScript. This allows us to selectively override inherited properties.

 

closure(闭包的概念)
函数可以被定义在其他函数中。一个内部函数自然可以访问自己的参数和变量,同时它也能方便地访问它被嵌套在其中的那个函数的参数与变量。通过函数字面量(非 new Function。。。的方式)创建的函数对象包含一个连到外部上下文的连接。这被称为闭包。它是JavaScript强大表现力的根基

JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked.

Each time a JavaScript function is invoked, a new object is created to hold the local variables for that invocation, and that object is added to the scope chain. 

When nested, however, function declarations may only appear at the top level of the function they are nested within. function defintion expressions may appear anywhere in you JavaScript code.

(var name = function declaration)

 

除了声明时定义的形式参数,每个函数接受两个附加的参数:this和arguments
方法调用模式
当一个函数被保存为对象的一个属性时,我们称它为一个方法。当一个方法被调用时,this被绑定到该对象。
函数调用模式
当一个函数并非一个对象的属性时,那么它被当做一个函数来调用。this被绑定到全局对象。 例如:
var sum = add(3, 4);

构造器调用模式
如果在一个函数前面带上new来调用,那么将创建一个隐藏连接到该函数对象的prototype成员的新对象,同时this将会被绑定到那个新对象上。
Apply调用模式
apply方法允许静态调用函数,并允许我们选择this的值。
apply方法接收两个参数。第一个是将被绑定给this的值。第二个就是一个参数数组。