javascript - Number 作为对象和 Number 作为构造函数之间的区别

标签 javascript constructor wrapper theory

当我们尝试在屏幕上打印 Number 时,例如:document.querySelector('#test').textContent = Number 我们得到结果:function Number() { [ native 代码] }。这就是构造函数方法的定义。但是为什么对象编号包装器 Number 使用相同的名称,它有几个方法和字段(属性),例如 Number.MAX_VALUE ? ..,即两者有什么区别?

最佳答案

只有一个 Number 对象。要理解其中的原因,您必须记住 JavaScript 的工作原理...看这个基本示例:

function Test(foo, bar) {
  this.foo = foo;
  this.bar = bar;
  
  return "Hello!";
}

Test.bye = function () {
  return "Bye!";
};

console.log(new Test('Foo', 'Bar'));
console.log(Test('Foo', 'Bar'));
console.log(Test.bye());

关于javascript - Number 作为对象和 Number 作为构造函数之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035811/

相关文章:

javascript - 我的表单 onSubmit = ""语法有点模糊

javascript - vscode 格式不格式化

c++ - 为什么不能在 C++ 中将构造函数声明为静态的?

c++ - 从复制构造函数调用构造函数时,它也会调用析构函数

java - JNI : How to handle the creation/removal of wrapped C++ object

php - 无法从一个 php 文件调用另一个 php 文件

javascript - 获取 dom 容器中的滚动百分比

c++ - 使用/不使用 vtable 初始化对象

c# - 为 C++ 类的复杂系统创建 C# 绑定(bind)?

css - 我的网站,它被剪掉了