- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
this
由执行上下文决定我习惯了this
的特点在 JavaScript 中。在以下示例中,this
由执行上下文决定。尽管getProtoPropViaThis
函数在 x
上定义, this
的值由函数的调用方式决定:
const xProto = {
protoProp: "x",
};
const x = {
getProtoPropViaThis() {
return this.protoProp;
},
}
Object.setPrototypeOf(x, xProto);
const yProto = {
protoProp: "y",
};
const y = {
getProtoPropViaThis: x.getProtoPropViaThis,
}
Object.setPrototypeOf(y, yProto);
console.log( x.getProtoPropViaThis() ); // Output: x
console.log( y.getProtoPropViaThis() ); // Output: y
super
不受执行上下文影响?我一直在使用 super
现在有一段时间了,但总是在类里面。所以我最近读到an article时感到很惊讶。切线地证明了super
似乎不遵循与 this
相同的规则.不知何故,在某种程度上我不完全理解(尽管多次阅读 ECMAScript 2021 language docs),super
设法保留其原始引用资料:
const xProto = {
protoProp: "x",
};
const x = {
getProtoPropViaSuper() {
return super.protoProp;
},
}
Object.setPrototypeOf(x, xProto);
const yProto = {
protoProp: "y",
};
const y = {
getProtoPropViaSuper: x.getProtoPropViaSuper,
}
Object.setPrototypeOf(y, yProto);
console.log( x.getProtoPropViaSuper() ); // Output: x
console.log( y.getProtoPropViaSuper() ); // Output: x
请注意 y
没有 x
或 xProto
在其原型(prototype)链中的任何位置,但它仍然是 xProto
调用 y.getProtoPropViaSuper()
时访问的属性
Object.assign
再举个例子,下面的根本不起作用:
const xProto = {
protoProp: "x",
};
const x = Object.assign(Object.create(xProto), {
getProtoPropViaSuper() {
return super.protoProp;
},
});
console.log(x.getProtoPropViaSuper());
super
的值是对象的原型(prototype),不是x
的,所以上面的例子只打印了 undefined
.
12.3.7.1 Runtime Semantics: Evaluation
SuperProperty :super. IdentifierName
- Let env be GetThisEnvironment().
- Let actualThis be ? env.GetThisBinding().
- Let propertyKey be StringValue of IdentifierName.
- If the code matched by this SuperProperty is strict mode code, let strict be true; else let strict be false.
- Return ? MakeSuperPropertyReference(actualThis, propertyKey, strict).
12.3.7.3 MakeSuperPropertyReference ( actualThis, propertyKey, strict )
The abstract operation MakeSuperPropertyReference takes arguments actualThis, propertyKey, and strict. It performs the following steps when called:
- Let env be GetThisEnvironment().
- Assert: env.HasSuperBinding() is true.
- Let baseValue be ? env.GetSuperBase().
- Let bv be ? RequireObjectCoercible(baseValue).
- Return a value of type Reference that is a Super Reference whose base value component is bv, whose referenced name component is propertyKey, whose thisValue component is actualThis, and whose strict reference flag is strict.
- Let envRec be the function Environment Record for which the method was invoked.
- Let home be envRec.[[HomeObject]].
- If home has the value undefined, return undefined.
- Assert: Type(home) is Object.
- Return ? home.[GetPrototypeOf].
最后:
8.1.1.3 Function Environment Records , Table 17
[[HomeObject]]
: If the associated function has super property accesses and is not an ArrowFunction, [[HomeObject]] is the object that the function is bound to as a method. The default value for [[HomeObject]] is undefined.
按照这个文档,好像getProtoPropViaSuper
应该绑定(bind)到 y
作为一种方法,但也许在 x
时以某种方式存储了绑定(bind)即使将函数分配给 y
,也会创建并保留对象.但是,我无法从这些文档中解析发生的时间和地点。
如果有人能用通俗易懂的语言解释这种行为,我将不胜感激。 super
如何确定它的值(value)?它看起来如何保持原来的状态 super
语境?如果它持有原始对象,这似乎会导致内存泄漏,因为原始对象无法被垃圾回收。但也许 super
引用是在准编译时确定的? (我说“准”是因为引用仍然可以被 Object.setPrototypeOf
改变)
最佳答案
perhaps that binding is somehow stored when the
x
object is created and retained even when the function is assigned toy
.
是的,这正是发生的事情。 getProtoPropViaSuper
方法基本上关闭了定义它的对象。这存储在函数本身的内部 [[HomeObject]] 槽中,这就是它被保留的原因如果您将该方法分配给不同的对象,或者 - 更重要的是 - 在不同的对象上继承它1。对于对象字面量中的方法定义,它是字面量创建的对象;对于 class
es 中的方法定义,它是类的 .prototype
对象。
1:为什么它需要是静态引用而不是像 Object.getPrototypeOf(this)
这样依赖于调用的东西,参见 here .
If it's holding onto the original object, it seems like that could cause memory leaks as the original object could not be garbage collected.
不,它不会导致比其他闭包更多的内存泄漏。当然,该方法阻止了它的 home 对象被垃圾收集,但是考虑到 home 对象是一个原型(prototype)对象——至少在正常使用中——它也在通常调用该方法的对象的原型(prototype)链中被引用,这不是问题。
I would really appreciate it if someone could explain this behavior in plain language. How does
super
determine its value?
它采用其绑定(bind)的 home 对象的原型(prototype),该对象是定义该方法的对象。但是请注意,访问 super
上的属性不会返回对属性的普通引用该对象,而是一个特殊的引用,当调用(方法)时,它将采用当前作用域的 this
值作为方法调用的 this
参数,而不是原型(prototype)对象.简而言之,
const x = {
method() {
super.something(a, b);
}
}
脱糖
const x = {
method() {
Object.getPrototypeOf(x).something.call(this, a, b);
}
}
和
class X {
method() {
super.something(a, b);
}
}
脱糖
class X {
method() {
Object.getPrototypeOf(X.prototype).something.call(this, a, b);
}
}
关于javascript - 与 'super' 相比,如何确定 'this' 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64395149/
我正在使用 Selenium Web 驱动程序 3.0,并且想要从打开的两个对话框(一个在后台,第二个在前台)的 Activity 对话框中单击“确定”按钮。如何从 html 下面的父 div 单击前
actions: [ FlatButton( onPressed: () {
我有一个问题有点超出我的范围(我真的很高兴我是 Beta)涉及重复项(所以 GROUP BY, HAVING, COUNT),通过将解决方案保留在 SQLite 附带的标准函数中而变得更加复杂。我正在
使用DBI是否可以确定SELECT语句的已执行语句句柄是否返回任何行而不从中获取行? IE。就像是: use DBI; ... my $sth = $dbh->prepare("SELECT ..."
是否可以为“确定”和“关闭”按钮指定回调函数? 如果是JQuery Modal,则可以在初始化时使用按钮字典指定回调函数。 Semantic-ui模态是否提供类似的功能?按下确定后,我该如何寻求其他逻
我想阅读警报中的消息。 示例:如果警报显示“错误的电子邮件地址”。怎么读呢?意味着我想将该消息存储在字符串中。 如何在“警报”中单击“确定”...?? 如何使用 Selenium 来做到这一点? 最佳
我有一个删除按钮: 我试图首先查明是否已选择一个网站,如果已选择一个网站,我需要确定是否已选择一个或多个列表项,如果是,则继续删除这些项目。 我的 if 语句不断返回“您必须首先选择您的列表”,即使它
部分出于好奇——我们想知道在我们的应用程序中发生了什么——部分是因为我们需要在我们的代码中找到一些潜在的问题,我喜欢在我们的网络应用程序运行时跟踪一些一般值。这尤其包括某些对象图的分配内存。 我们的应
我将 SweetAlert 与 Symfony 结合使用,我希望用户在完成删除操作之前进行确认。 发生的情况是,当用户单击删除按钮时,SweetAlert 会弹出,然后立即消失,并且该项目被删除。 在
我们有一个应用程序可以生成不包括字母 O 的随机基数 35 [0-9A-Z]。我正在寻找一种解决方案来查找包含任何淫秽英语单词的代码,而无需搜索包含 10,000 个条目的列表每个生成的代码。每秒生成
这是我做的: #include #include int betweenArray(int a, int b){ int *arr,i,range; range = b - a +
我知道如何创建 警报和确认框,但我不知道如何做的是实际单击“确定”。我有一个弹出确认框的页面。 我想使用 Java Script 插件单击“确定”。基本上,我希望我的代码单击页面上的链接,然后在出现提
代码: swal('Your ORDER has been placed Successfully!!!'); window.location="index.php"; 甜蜜警报工
>>> import re >>> s = "These are the words in a sentence" >>> regex = re.compile('are|words') >>> [m
使用确定的理想散列函数给出随机期望线性时间算法两个数组 A[1..n] 和 B[1..n] 是否不相交,即 A 的元素是否也是 B 的元素。 谁能告诉我如何做到这一点,甚至如何开始考虑它? 最佳答案
我在计算机科学课上有这段代码: int input=15; while (input < n ) { input = input *3;} 这段代码有 log3(n/15) 次循环的上限。我们怎样才能
我有一个允许 2 位玩家玩 TicTacToe 的程序。在每个玩家移动之后,它应该在那个点显示棋盘并返回一个名为 Status 的枚举,显示玩家是否应该继续,如果玩家赢了,还是平局。但是,该算法要么返
给定一个 y 值数组,例如 [-3400, -1000, 500, 1200, 3790],我如何确定“好的”Y 轴标签并将它们放置在网格上? ^ ---(6,000)-|---
假设我有一个检查用户登录的 SQL 语句: SELECT * FROM users WHERE username='test@example.com', password='abc123', expi
teradata中有返回表中哪一列被定义为主索引的命令吗?我没有制作一些我正在处理的表,也没有尝试优化我对这些表的连接。谢谢! 最佳答案 有dbc.IndicesV,其中IndexNumber=1表示
我是一名优秀的程序员,十分优秀!