JavaScript“类” [英] JavaScript "classes"

查看:109
本文介绍了JavaScript“类”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript类与此模式有任何缺点吗?

  var FooClass = function $ b {
var private =a private variable;
this.public =a public variable;

var privatefn = function(){...};
this.publicfn = function(){...};
};

var foo = new FooClass();
foo.public =bar;
foo.publicfn();


解决方案

人们在JS中想到的类模式 - 通常人们都在想象更多的正常的Java / C#/ C ++ /等类模型,它们可以用库来伪造。



相反,你的例子实际上是相当正常和良好的JS设计,但为了完整性,我会讨论你会看到的私人和公共成员之间的行为差​​异

  var private =a private variable; 
this.public =a public variable;

从任何功能中访问 private 将比访问 public 快很多,因为 private 的位置可以很好地确定,只需使用静态查找由JS引擎。尝试访问 public 需要查找,大多数现代JS引擎执行一定程度的查找缓存,但它仍然比简单的作用域var访问更昂贵。

  var privatefn = function(){...}; 
this.publicfn = function(){...}

相同的查找规则适用于以上变量访问的这些函数,你的例子)是如果你的函数被调用,说 privatefn() vs this.publicfn() privatefn 将总是获得 this 的全局对象。

  f = foo.publicfn; 
f();

然后调用 f 全局对象 this ,它将能够修改 private p>

然而,做公共函数的更正常的方法(解析分离的公共函数修改私有成员问题)是将公共函数放在原型上,例如

  Foo.prototype.publicfn = function(){...} 

这迫使公共函数不修改私人信息 - 有些时候这不是一个选项,但它是一个好习惯,因为它也稍微减少内存使用,采取: / p>

  function Foo1(){
this.f = function(){returnfoo};
}

vs

  function Foo2(){
}
Foo2.prototype.f = function(){returnfoo};

Foo1 函数对象为 Foo1 (不是所有的emory,只是对象,例如 new Foo1()。f!== new Foo2 ).f ),而在 Foo2 中只有一个函数对象。


Are there any downsides to using a JavaScript "class" with this pattern?

var FooClass = function()
{
  var private = "a private variable";
  this.public = "a public variable";

  var privatefn = function() { ... };
  this.publicfn = function() { ... };
};

var foo = new FooClass();
foo.public = "bar";
foo.publicfn();

解决方案

What you're doing in your example isn't the "class" pattern people think of in JS -- typically people are thinking of the more "normal" class model of Java/C#/C++/etc which can be faked with libraries.

Instead your example is actually fairly normal and good JS design, but for completeness i'll discuss behaviour differences you'll see between the private and public "members" you have

var private = "a private variable";
this.public = "a public variable";

Accessing private from within any of your functions will be quite a lot faster than accessing public because the location of private can be determined reasonably well just with a static lookup by the JS engine. Attempts to access public require a lookup, most modern JS engines perform a degree of lookup caching, but it is still more expensive than a simple scoped var access.

var privatefn = function() { ... };
this.publicfn = function() { ... };

The same lookup rules apply to these functions as with the above variable accesses, the only real difference (in your example) is that if your functions are called, say privatefn() vs this.publicfn(), privatefn will always get the global object for this. But also if someone does

f = foo.publicfn;
f();

Then the call to f will have the global object as this but it will be able to modify the private variable.

The more normal way to do public functions however (which resolves the detached public function modifying private members issue) is to put public functions on the prototype, eg.

Foo.prototype.publicfn = function() { ... }

Which forces public functions to not modify private information -- there are some times where this isn't an option, but it's good practice as it also reduces memory use slightly, take:

function Foo1() {
    this.f = function(){ return "foo" };
}

vs

function Foo2() {
}
Foo2.prototype.f = function(){ return "foo" };

In Foo1 you have a copy of the function object for every instance of Foo1 (not all the emory, just the object, eg. new Foo1().f !== new Foo2().f) whereas in Foo2 there is only a single function object.

这篇关于JavaScript“类”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆