JS prototype and JS object

来源:互联网 发布:it维修单 编辑:程序博客网 时间:2024/06/01 13:09

类与构造函数

1.构造函数的prototype属性被用作新对象的原型。

<pre class="javascript" name="code">function Range(from, to) {// Store the start and end points (state) of this new range object.// These are noninherited properties that are unique to this object.this.from = from;this.to = to;}// All Range objects inherit from this object.// Note that the property name must be "prototype" for this to work.Range.prototype = {// Return true if x is in the range, false otherwise// This method works for textual and Date ranges as well as numeric.constructor: Range, // Explicitly set the constructor back-referenceincludes: function(x) { return this.from <= x && x <= this.to; },// Invoke f once for each integer in the range.// This method works only for numeric ranges.foreach: function(f) {for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);},// Return a string representation of the rangetoString: function() { return "(" + this.from + "..." + this.to + ")"; }};// Here are example uses of a range objectvar r = <strong>new Range</strong>(1,3); // Create a range objectr.includes(2); // => true: 2 is in the ranger.foreach(console.log); // Prints 1 2 3console.log(r); // Prints (1...3)

2.构造函数及其原型和实例


函数定义表达式 VS 函数声明语句

Function definition expression VS Function declaration statement

1.

1.1) Function definition expression(对函数定义表达式来说,函数名称是可选的)

<span style="font-size:14px;">var square = function(x) { return x * x; }</span>

1.2) Function declaration statement

<span style="font-size:14px;">function hypotenuse(x, y) {return Math.sqrt(x*x + y*y); // return is documented in the next section}</span>

1.3) Function Constructor(在实际中很少使用,它允许JS在运行时动态创建并编译函数)

var f=new Function("x","y","return x*y;");

此函数与通过函数定义表达式定义的函数几乎是等价的。

优先选择Function definition expression。理由如下:

Function declaration statements differ from function definition expressions in that they
include a function name. Both forms create a new function object, but the function
declaration statement also declares the function name as a variable and assigns the
function object to it. Like variables declared with var, functions defined with function

definition statements are implicitly “hoisted” to the top of the containing script or
function, so that they are visible throughout the script or function. With var, only the
variable declaration is hoisted—the variable initialization code remains where you
placed it. With function declaration statements, however, both the function name and
the function body are hoisted: all functions in a script or all nested functions in a function
are declared before any other code is run. This means that you can invoke a Java-
Script function before you declare it.

尽管函数声明语句和函数定义表达式包含相同的函数名,但两者仍然不同。两种方式都创建了新的

函数对象,但函数声明语句中的函数名也被声明为一个变量名,变量指向函数对象。和通过var声明变量一样,

函数定义语句中的函数被显式地“提前”到了脚本或函数的顶部。因此它们在整个脚本和函数内部都是可见的。使用var的话,

只有变量声明提前了——变量的初始化代码仍然在原来的位置。然而使用函数声明语句的话,函数名称和函数体均提前:脚本中的所有函数和函数中所有嵌套的函数都会在当前上下文中其他代码之前声明。也就是说,可以在声明一个JS函数之前调用它。

2.JS Object

2.1) Object Literals

var book = {"main title": "JavaScript", // Property names include spaces,'sub-title': "The Definitive Guide", // and hyphens, so use string literals"for": "all audiences", // for is a reserved word, so quoteauthor: { // The value of this property isfirstname: "David", // itself an object. Note thatsurname: "Flanagan" // these property names are unquoted.}};

2.2) new

var o = new Object(); // Create an empty object: same as {}.var a = new Array(); // Create an empty array: same as [].var d = new Date(); // Create a Date object representing the current timevar r = new RegExp("js"); // Create a RegExp object for pattern matching.


2.3) Object.create()

第一个参数是原型对象,第二个参数是可选的;

var o1 = Object.create({x:1, y:2});




0 0