javascript权威指南(07~08)

7.2.2. 检验属性是否存在

if ("x" in o) o.x = 1;
if (o.x !== undefined) o.x = 1;

7.3. 属性访问

object.property;object["property"]

在json中相关的定义也可以不是字符串,如{'100',height:'200',color:'ddddd'}等价 {'width':'100','height':'200','color':'ddddd'}

7.4.5. hasOwnProperty() 方法

指对象自己本地定义的属性,原型链上继承来的是不属于的

7.4.6. propertyIsEnumerable() 方法

返回值同hasOwnProperty(),指对象自己本地定义的属性,原型链上继承来的是不属于的,可列举的属性

7.4.7. isPrototypeOf() 方法

JavaScript 中isPrototypeOf函数方法是返回一个布尔值,指出对象是否存在于另一个对象的原型链中。使用方法:object1.isPrototypeOf(object2)

如果 object2 的 原型链中包含object1,那么JavaScript中isPrototypeOf函数方法返回 true。原型链可以用来在同一个对象类型的不同实例之间共享功能。如果 object2 不是一个对象或者 object1 没有出现在 object2 中的原型链中,JavaScript中isPrototypeOf函数方法将返回 false。

var o = {}
Object.prototype.isPrototypeOf(o); // true: o.constructor == Object
Object.isPrototypeOf(o); // false
o.isPrototypeOf(Object.prototype); // false
Function.prototype.isPrototypeOf(Object); // true: Object.constructor==Function

7.6. 读写数组元素

相关元素索引应为[0,232-1],其他情况下都是在数组对象上面添加的相关的属性

7.6.2. 删除 Array 元素

删除元素的方式,如果通过delete操作符,只是设置undefined 值,需要通过方法shift(),pop(),splice()

8.1.2. 函数定义

匿名函数的定义,并将函数引用传递给变量,和直接函数定义存在一定的差距。主要是要考虑js的编译执行的过程。

function hypotenuse(a, b) {
    function square(x) { return x*x; };
    return Math.sqrt(square(a) + square(b));
}
function hypotenuse(a, b) {
    return Math.sqrt(square(a) + square(b));
    function square(x) { return x*x; };
}
function hypotenuse(a, b) {
    return Math.sqrt(square(a) + square(b));
    var square =function (x) { return x*x; };
}

匿名函数的定义方式也有许多运用的好处。

f[0] = function(x) { return x*x; };  // Define a function and store it
a.sort(function(a,b){return a-b;});  // Define a function; pass it to another
var tensquared = (function(x) {return x*x;})(10);  // Define and invoke

8.4. 函数和方法

函数调用一般为全局对象调用的。方法则是对象内定义的函数,是属于对象的。

var name="win";
function o()
{
    this.name="0";
    this.a=function ()
    {
        alert(this.name);
        var name="a";
        function b()
        {
            alert(this==window);
            alert(this.name);
            alert(name);//这个是通过定义域链//a
        };
        b();//内联函数,都是window对象调用的。
        this.e=b;//实例的属性e
        this.e();
    };
};
var d=new o();
d.a();
d.e();

8.6.3. 定义函数静态变量

uniqueInteger.counter = 0;
function uniqueInteger() {
    // Increment and return our "static" variable所有函数共享
    return uniqueInteger.counter++;
}
 // 改成这种是不是好些
function uniqueInteger() {
    uniqueInteger.counter = uniqueInteger.counter||0;
    return uniqueInteger.counter++;
} 

8.9. Function() 构造器

函数调用一般为全局对象调用的。方法则是对象内定义的函数,是属于对象的。

var f = new Function("x", "y", "return x*y;");可以动态创造函数,因为相关的可以字符串作为参数。注意函数是不存在相关的定义域的

var y = "global";
function constructFunction() {
    var y = "local";
    return new Function("return y");  // Does not capture the local scope!
}
// This line displays "global" because the function returned by the
// Function() constructor does not use the local scope. Had a function
// literal been used instead, this line would have displayed "local".
alert(constructFunction()());  // Displays "global"
原文地址:https://www.cnblogs.com/legu/p/1692807.html