我是一名最近进入 JavaScript 世界的 C++ 程序员;现在,为了我的理解和心理健康,我正在尝试将 C++ 的一些设计模式应用于 JavaScript。
AFAIK,以下代码在 C++ 和 Javascript 中是等价的:
C++
// Class definition
template <typename T> class foo
{
public:
// Constructor
foo(T value) { this->value = value; }
// Public function
T twice() { return this->value + this->value; }
private:
// Private function
void bar() { }
// Private member
T value;
};
JavaScript
// "Class" definition and constructor
function foo(value)
{
// "Private" member
this.value = value;
// "Private" function
this.bar = function() { };
}
// Public function
foo.prototype.twice = function() { return this.value + this.value; };
这两个类的用法也很相似:
C++ live demo
foo<int> f1(1);
foo<std::string> f2("1");
std::cout << f1.twice() << '\n'; // output: 2
std::cout << f2.twice() << '\n'; // output: 11
JavaScript live demo
var f1 = new foo(1);
var f2 = new foo('1');
print(f1.twice()); // output: 2
print(f2.twice()); // output: 11
但是有一件事情是 JavaScript 类无法做到的,而 C++ 类却可以做到:使用临时 RValue 来完成任务:
C++
std::cout << foo<float>(3.14f).twice() << '\n'; // output: 6.28
JavaScript
print(foo(3.14).twice()); // Uncaught TypeError: undefined is not a function
我认为 JavaScript 版本的错误是由于 foo 是一个函数并且它什么都不返回(undefined
),所以一开始我想用代码更改构造函数下面:
JavaScript
// "Class" definition and constructor
function foo(value)
{
// "Private" member
this.value = value;
// "Private" function
this.bar = function() { };
return this; // <----- new code!
}
但这根本行不通; return this;
指令返回的对象不是 foo
类型(foo(3.14) instanceof foo
是 false
)。
在 Chrome 35.0.1916.114 中调试时,指令 return this;
中 this
的类型是 foo
但类型更改为 window
在这种情况下:
var x = foo(3.14); // x is typeof window
介绍完毕,问题来了:
- 为什么
this
的类型在构造函数内部是foo
而在外部捕获时是window
?- 是因为没有使用
new
运算符吗?
- 是因为没有使用
- 有没有办法创建行为类似于 C++ RValues 的 JavaScript 对象?
最佳答案
在 JavaScript 中,当您对函数使用 new
关键字时,函数的行为与不使用 new
关键字时的行为不同。有了它,函数就像一个类,并从中实例化一个新对象,就像在传统的 OOP 语言中一样。此外,this
上下文设置为自身,函数的返回值被忽略。
相比之下,如果没有 new
关键字,则 this
上下文设置为全局范围,对于浏览器而言,它是 window
对象,并且返回值可以被任何调用它的东西捕获。
可以创建类似于您的 C++ 示例的东西,您不需要 new
关键字,它仍然返回一个新对象。
function foo(value) {
function foo(value) {
this.value = value;
this.bar = function () {};
this.twice = function() { return this.value + this.value; }
}
return new foo(value);
}
console.log( foo(3.14).twice() ); // 6.28
说明:
外部函数 foo
的行为类似于普通函数,并且旨在在没有 new
关键字的情况下调用。内部存在内部 foo
,它旨在类似于一个类 - 使用 new
关键字调用。外部 foo
实例化内部 foo
的新实例并返回它。因此,它可以像 C++ 示例一样使用。类函数不需要在外层函数中声明,只需要在外层函数中封装即可。
可见性
问题中的两个示例并不完全等效,因为 JavaScript 示例使用所有公共(public)属性和方法,而 C++ 示例将 bar
和 value
作为私有(private)。
下面是一个更接近C++版本的版本:
function foo(value) {
function foo(value) {
var value = value;
function bar(){}
this.twice = function() { return value + value; }
}
return new foo(value);
}
console.log( foo(3.14).twice() ); // 6.28
console.log( foo(3.14).value ); // undefined because it's private
console.log( foo(3.14).bar() ); // undefined because it's private
从测试用例中可以看出,value
和 bar
不可公开访问/可见。这是通过不使用 this
前缀并使用 var
关键字(定义局部变量)声明 value
来实现的。该函数被声明为声明而不是表达式。在 JavaScript 中,没有像 OOP 语言那样声明/区分公共(public)和私有(private)属性或方法的正式方法。
你可能已经注意到上面的例子没有使用原型(prototype)来声明任何方法。这样做的原因是原型(prototype)方式总是会声明具有公共(public)可见性的方法,而原型(prototype)方法不能从“类”或函数内部看到任何变量。问题中的代码注释表明在函数体内使用 this.bar
会使其私有(private)化,但事实并非如此。
关于javascript - 有没有办法创建行为类似于 C++ RValues 的 JavaScript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035431/