- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
方法(对象的函数)可以使用“this”关键字来引用对象的变量。
函数的属性可以引用函数变量吗?
例如:-
function foo()
{
var x=5;
}
foo.help = {o1:x};// o1 is not initialized to x.
//Using o1:this.x also doesn't help. Why? Explain please.
// 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)
最佳答案
编辑
此答案基于已编辑的问题-原始答案如下
你的理解有点落后。
该函数没有您认为的 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
methods
上的
range
不是函数的一部分。
r = inherit(range.methods);
调用中,该代码块中没有解释。
this
根本不涉及该函数。
range_3_to_5
和
range_6_to_12
都使用相同的
includes
副本。
range_3_to_5.includes(6);
时,将
this
设置为
range_3_to_5
,然后函数使用
this.from
和
this.to
来判断
x
是否在范围内。
this
来确定他们当时正在处理的副本。
var car = { license : "a32 vx98" },
truck = { license : "vdx 2000" },
jeep = { license : "mkv 3a2b" };
var read_license = function () { console.log(this.license); };
car.read_license = read_license;
truck.read_license = read_license;
car.read_license(); // [logs:] a32 vx98
truck.read_license(); // [logs:] vdx 2000
.call
或
.apply
方法手动设置
this
。
read_license.call(jeep); // [logs:] mkv 3a2b
.bind
来保存函数的版本,该版本总是使用相同的值
this
。
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
{}
对象添加属性一样向它们添加东西。
var myFunc = function () {
var myValue = 23;
myFunc.properties = {};
myFunc.properties.value = myValue;
};
myFunc();
console.log(myFunc.properties.value); // [logs:] 23
this
无关。
this
用于与您要查找的内容相反的内容。
var myObject = {
x : 23,
y : 42,
sayX : function () { console.log(this.x); },
sayY : function () { console.log(this.y); }
};
myObject.sayX(); // [logs:] 23
this
用于其他几个地方,但实际上,这是它的主要作用:访问或设置函数附加到的对象上的值/方法(通过点属性访问
obj.func()
或通过手动设置,使用
.call
/
.apply
/
.bind
),另一个非常常见的情况是使用
new
关键字创建新对象。
x
工作的方法不是找出
this
,而是将其设置在函数本身中,或者更恰本地,将
x
(返回
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;
this
仅适用于函数内部
var obj = {};
obj.x = 12;
obj.y = this.x + 5; // DOESN'T WORK
this
在最后一个示例中有效,那只是因为它在函数内部使用
this
,该函数将引用 FUNCTION 的
this
,而不是
obj
。如果您正在调用未附加到对象的函数(
obj.func();
或
func.call(obj)
或
new_func = func.bind(obj);
),那么
this
将指向
window
。
关于JavaSCript 示例(Oreilly 书籍),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038119/
揭开网络爬虫的神秘面纱 嗨,亲爱的读者朋友们,今天我想和大家分享一下我对Python3网络爬虫书籍的一些心得体会。网络爬虫就像是一位善于探险的探险家,穿越数字世界的丛林,捕捉着信息的脉搏。 历经荆棘,
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是 JavaScipt 新手,我无法理解下面的代码 maskat.lang.Class.declare("maskat.key.KeyEventManager", { _static: {
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在开发一个应用程序,允许用户用户查看他们有权访问的收件箱的内容。除了 之外,我很难找到一种搜索全局地址列表的方法 AddressEntries entries = global.AddressEn
人们常常问我有什么C++和编程的书籍推荐,今天就为大家分享了几本 第一个注意项:如果你打算学习C++,请务必学习最新版的C++ 2011。这个版本的C++移除了许多由C++强大带来的一些痛苦之处。
我需要为一本书创建一个索引。虽然乍一看这项任务很简单——按第一个字母对单词进行分组,然后对它们进行排序——但这个显而易见的解决方案仅适用于美国语言。然而,真正的词要复杂得多。参见 http://en.
我是一名开发人员,将我们的 IIS 服务器的管理职责交给了他。在设置网站、虚拟目录等方面,我对 IIS 相当熟悉,但我想如果我要完全成为服务器的管理员,我最好尽快跟上。 有什么关于 IIS 书籍的推荐
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
如果我在网站上看到很酷的效果,我会查看 jquery 的源代码,通常我可以理解它,但是,我正在查看 http://www.mckinsey.com 的 slider 。并查看了用于进行页面幻灯片的 j
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
方法(对象的函数)可以使用“this”关键字来引用对象的变量。 函数的属性可以引用函数变量吗? 例如:- function foo() { var x=5; }
我有以下型号。 Book has Articles (Article has foreign key to Book) Article has Images (Article has upto #ma
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我是一名优秀的程序员,十分优秀!