Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSCript示例(Oreilly书)_Javascript_This - Fatal编程技术网

JavaSCript示例(Oreilly书)

JavaSCript示例(Oreilly书),javascript,this,Javascript,This,方法(对象的函数)可以使用“this”关键字引用对象的变量 函数的属性是否可以引用函数变量 例如:- 是否仍要将o1初始化为x 例9-1。一个简单的JavaScript类 // range.js: A class representing a range of values. // This is a factory function that returns a new range object. function range(from, to) { // Use the inherit()

方法(对象的函数)可以使用“this”关键字引用对象的变量

函数的属性是否可以引用函数变量

例如:-

是否仍要将o1初始化为x

例9-1。一个简单的JavaScript类

// range.js: A class representing a range of values.
// This is a factory function that returns a new range object.
function range(from, to) {
// Use the inherit() function to create an object that inherits from the
// prototype object defined below. The prototype object is stored as
// a property of this function, and defines the shared methods (behavior)
// for all range objects.
var r = inherit(range.methods);
// Store the start and end points (state) of this new range object.
// These are noninherited properties that are unique to this object.
r.from = from;
r.to = to;
// Finally return the new object
return r;
}
// This prototype object defines methods inherited by all range objects.
range.methods = {
// Return true if x is in the range, false otherwise
// This method works for textual and Date ranges as well as numeric.
includes: 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 range
toString: function() { return "(" + this.from + "..." + this.to + ")"; }
};
// Here are example uses of a range object.
var r = range(1,3); // Create a range object
r.includes(2); // => true: 2 is in the range
r.foreach(console.log); // Prints 1 2 3
console.log(r); // Prints (1...3)
//range.js:表示一个值范围的类。
//这是一个返回新范围对象的工厂函数。
功能范围(从、到){
//使用inherit()函数创建从
//原型对象定义如下。原型对象存储为
//此函数的属性,并定义共享方法(行为)
//适用于所有范围对象。
var r=inherit(range.methods);
//存储此新范围对象的起点和终点(状态)。
//这些是此对象特有的非继承属性。
r、 from=from;
r、 to=to;
//最后返回新对象
返回r;
}
//此原型对象定义所有范围对象继承的方法。
range.methods={
//如果x在范围内,则返回true,否则返回false
//此方法适用于文本和日期范围以及数字范围。

includes:function(x){return this.from在上下文
foo.help={o1:x}
中,您与
x
的定义不在同一词汇范围内,后者将位于
foo
函数范围内。这也适用于
this.x
(适用于对象范围)

编辑 此答案基于编辑后的问题——原始答案如下


你的理解有点落后。
该函数没有您认为的
from
to

如果我以不同的方式构建它,以获得几乎所有相同的功能,但对于一个JS新手来说,理解起来要容易得多,我会这样写:

// my `makeRange` function makes an object
// then it gives it all of the things a range object needs
// then it returns the new object
var makeRange = function (min, max) {
    // there are cleaner more-compact ways of doing this
    // I'm writing this out in long-form to show you exactly what's going on
    var rangeObject = {};
    rangeObject.from = min;
    rangeObject.to = max;
    rangeObject.includes = includes;
    rangeObject.foreach  = foreach;
    rangeObject.toString = toString;

    return rangeObject;
};

// these are all of the functions inside of `range.methods`
// they don't have to be attached to the function ***AT ALL***, for ***ANY REASON***
// other than the author wanted them to be there for the sake of organization
// Here, I'm just putting them out on their own, for sake of clarity
var includes = function (x) { return this.from <= x && x <= this.to; },
    foreach  = function (func) {
        var min = this.from,
            max = this.to,
            i   = 0;

        for (i = min; i <= max; i += 1) { func(i); }
    },
    toString = function () { return "(" + this.from + "..." + this.to + ")"; };



var range_3_to_5  = makeRange(3,  5),
    range_6_to_12 = makeRange(6, 12);


range_3_to_5.from;         // 3
range_6_to_12.includes(8); // true
range_6_to_12.foreach(function (i) { console.log(i); }); // [logs:] 6,7,8,9,10,11,12
我甚至可以使用函数的
.call
,自己调用函数。应用
方法手动设置

read_license.call(jeep); // [logs:] mkv 3a2b
var read_car_license = read_license.bind(car);

read_car_license();  // a32 vx98
或者使用
.bind
保存一个版本的函数,该函数始终使用与
相同的值

read_license.call(jeep); // [logs:] mkv 3a2b
var read_car_license = read_license.bind(car);

read_car_license();  // a32 vx98
下面是先前的答复


一点也不

变量位于创建它们的函数中:

var myFunction = function () {
    var myValue = 23;
    console.log(myValue);
};


myFunction(); // [log:] 23
console.log(myValue); // undefined
值可以存在于函数内部,具体如下:

var myFunction = function () {
    var myValue = 23,

        sayValue = function () {
            console.log(myValue);
        };


    sayValue();  // will log 23 when you call `myFunction`
}



myFunction();  // [log:] 23
但是,如果希望变量位于函数外部(而不是更内部),则需要将值返回到某个值,或者直接从函数内部将其设置为某个值

var myOutsideValue = 42,

    myFunction = function () {
        var myValue = 23;
        myOutsideValue = myValue;
    };



console.log(myOutsideValue); // 42
myFunction();
console.log(myOutsideValue); // 23
或返回

var myReturnedValue = 0,
    myFunction = function () {
        var myValue = 23;
        return myValue;
    };

myReturnedValue = myFunction();
console.log(myReturnedValue); // [log:] 23
也可以传入要修改的数组或对象:

var myObject = {},

    myFunction = function (obj) {
        var myValue = 23;
        obj.value = myValue;
    };


myFunction(myObject);
console.log(myObject.value);  // [log:] 23
功能可以引用自身。
因为在JavaScript中,函数是对象(可以有自己的属性和方法),所以可以像向任何
{}
对象添加属性一样向它们添加内容

var myFunc = function () {
    var myValue = 23;
    myFunc.properties = {};
    myFunc.properties.value = myValue;
};


myFunc();
console.log(myFunc.properties.value); // [logs:] 23
所有这些都与
这件事无关

read_license.call(jeep); // [logs:] mkv 3a2b
var read_car_license = read_license.bind(car);

read_car_license();  // a32 vx98
用于与您所寻找的相反的用途。
它适用于当您在附加到对象的函数中,并且希望读取该对象上的其他属性/运行该对象上的其他方法时

var myObject = {
    x : 23,
    y : 42,
    sayX : function () { console.log(this.x); },
    sayY : function () { console.log(this.y); }
};


myObject.sayX(); // [logs:] 23
用于其他几个点,但实际上,这是它的主要作用:访问或设置函数附加到的对象上的值/方法(通过点属性访问
obj.func()
,或通过手动设置,使用
。调用
/
。应用
/
.bind
),另一种非常常见的情况是使用
new
关键字创建新对象

因此,让
x
工作的方法不是找出
这个
,而是在函数本身中设置它,或者更恰当地说,将
x
传递到另一个变量外部(return
x
),然后自己设置值

var foo = function () {
    var x = "x";
    return x;
},


variable;  // === undefined

foo.help = { o1 : 0 };

variable = foo(); // variable === "x"
foo.help.o1 = variable;


// or shorter:  foo.help.o1 = foo();
备选方案:

var help = { o1 : 0 },

    foo = function (obj) {
        var x = "x";
        obj.o1 = x;
    };



foo(help);
foo.help = help;
仅在函数内部工作

var obj = {};

obj.x = 12;
obj.y = this.x + 5;  // DOESN'T WORK

如果
this
在上一个示例中起作用,那只是因为它从一个函数内部使用
this
,该函数将引用该函数的
this
,而不是
obj
。如果调用的函数未附加到对象(
obj.func();
func.call(obj))
新建函数=函数绑定(obj);
),然后,
这个
将指向
窗口

编辑问题标题。因为在这个范围内,
这个
不是指
foo
@Blender我如何将o1的值初始化为x?非常感谢。请再次查看问题。@Amit0191这是你编辑过的答案。这里没有什么神奇:
范围
是根本不需要使用
this
。方法是为它生成的对象提供的(因为它生成并返回范围对象)。阅读答案了解更多信息…有些函数附加到对象上,这就是为什么“this”关键字。如果函数单独使用,则该关键字将指向全局对象。您认为我已经完全理解了这个概念,还是缺少了什么?非常感谢。(我想我已经理解了)