- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Feel free to skip to the actual question below, if you find the'backstory' here unnecessary. But I do believe it adds good amount ofdetail and further context for the question
最近我一直在尝试对象和 [[ Prototype ]]
链,
首先,我很困惑为什么我的 this
引用会以 NaN
的形式返回,代码如下:
var obj = {
a: 2
}
Object.defineProperty(obj, 'b', {
value: this.a + 2,
enumerable: true
});
console.log(obj.b);
首先,我对为什么感到困惑,但后来发生的事情让我意识到了 NaN
行为。
我的 obj.b
计算为 undefined + 2
,因此返回 NaN
现在,显然更改代码对 value: obj.a + 2
进行显式引用就可以解决问题,但我想进一步调查为什么 this.a
> 返回为未定义
。
基本上,我的问题是我的调用堆栈引用了Object.prototype.a
,因为Object.defineProperty
是隐式委托(delegate)的Object.prototype.defineProperty
.
因此,明确将 a
设置为 Object.prototype
的属性可以解决此问题:
var obj = {
a: 2
}
Object.prototype.a = obj.a;
Object.defineProperty(obj, 'b', {
value: this.a + 2,
enumerable: true
});
console.log(obj.b);
这将返回 4
的预期结果(尽管是的,但为整个 Object.prototype
创建属性 a
绝对不是最好的编程实践,但出于说明目的,它就可以了)
但是,令我困惑的是,如果我在表达式上运行 console.trace
,我的调用堆栈纯粹由 (anonymous)
组成 - 所以基本上 global
(默认)this
关键字的隐式绑定(bind)。
You will have to run the code in your own console, as sadly
console.trace()
is unfortunately not supported by jsfiddle (least to my knowledge)
var obj = {
a: 2
}
Object.prototype.a = obj.a;
console.trace(Object.defineProperty(obj, 'b', {
value: this.a + 2,
enumerable: true
}));
<小时/>
因此,将此问题总结为两个要点:
(匿名)
,而不返回Object.prototype
this
显式绑定(bind)到 .defineProperty()
方法中引用的对象 obj
的正确方法是什么?提前谢谢您。
最佳答案
一旦您意识到自己错误地使用了 console.trace(),这种行为就会变得明显。如果您查阅 Mozilla 手册并查找 console.trace(),您会发现它应该这样使用:
function foo() {
function bar() {
console.trace();
}
bar();
}
foo();
输出如下:
bar
foo
<anonymous>
换句话说,您必须在某个函数内调用 console.trace(),而不是用它包装函数调用。这是因为 console.trace() 首先检查调用它的函数的名称 (bar),然后检查父调用函数的名称 (foo),然后其父级的名称,依此类推,直到到达最外层的执行上下文,这是一个匿名函数 - 主执行线程,所有代码都从这里开始。
每当你在 devtools 中执行一些代码(我假设你正在使用它)时,它总是在这个全局执行上下文中以包装匿名函数的形式执行。由于您立即从此匿名函数调用 console.trace(),因此打印的内容是 - anonymous。
此外,console.trace() 还接受参数。当您调用 console.trace() 时,可以使用任意数量的参数,并且这些参数只是在堆栈跟踪之前打印到控制台。由于 Object.defineProperty() 返回您为其定义新属性的对象,因此这就是您传递给 console.trace() 的内容。
TL;DR - console.trace() 首先将其参数打印到控制台(即 obj,即 {a: 2, b: 4});然后,它继续打印堆栈跟踪,该跟踪仅包含主执行上下文,当您从控制台调用代码时,它是一个匿名函数。
Object.defineProperty 的第三个参数是一个简单对象,不是函数!它的属性在初始化期间立即解析,然后才传递到defineProperty。这意味着以下代码在功能上与您的代码相同:
// First we initialize the third argument, separately from the call
// to .defineProperty()
var descriptor = {
value: this.a + 2,
enumerable: true
};
// at this point descriptor is already constructed. If you do
// console.log(descriptor.value) here, you will get NaN or maybe
// 4 or something else, depending on what you did before
Object.defineProperty(obj, 'b', descriptor);
// Here we simply invoke .defineProperty with descriptor as the third argument.
您还必须知道,当您在主执行上下文中运行代码时(例如从开发工具控制台调用代码),this 指的是 Window 对象。 Window 没有属性 a,这就是 this.a 未定义的原因。
更有趣的是,在执行 Object.prototype.a = 2 后,this.a(或 window.a)变为 2。发生这种情况是因为 Window 毕竟仍然是一个对象。如果您将某个属性分配给对象原型(prototype)对象(!!!),则该属性将在代码中的所有对象上可用!令人困惑?是的!现在只需说您打算做的事情(根据您的描述)可能是这样的:
obj.prototype = {a: 2};
或者这个:
var proto = {}; // First create a prototype
obj.prototype = proto; // Then assign it to the prototype property of your obj
obj.prototype.a = 2; // Now we can alter props on the prototype
看出区别了吗?这更有意义吗?当然,执行此操作后,您的代码将不再工作,因为 this.a 将再次变为未定义。
结果如下:按照这种方式,您无法实现您期望的行为。属性描述符(参数#3)实际上比值和可枚举的组合更强大,如果你实现了 set( ) & get() 方法。另外,我相信您可以使用代理来获得类似的行为。不提供更多细节,因为我建议您首先阅读函数执行上下文,然后再阅读更多关于原型(prototype)的内容,也许还有一些关于堆栈溢出的关于 this 行为的好问题。之后,您可以尝试研究建议的解决方案。
关于javascript - .defineProperty 方法期间调用堆栈和 this 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078932/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!