freeCodeCamp/guide/chinese/javascript/object-instantiation/functional/index.md

151 lines
5.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Functional
localeTitle: 实用
---
```javascript
var fun = function(a, b) {
var funInstance = {};
funInstance.a = a;
funInstance.b = b;
funInstance.method1 = function() {
// method code here
}
funInstance.method2 = function() {
// method code here
}
funInstance.method3 = function() {
// method code here
}
return funInstance;
}
var myFun = fun(1, 2);
myFun.method1();
```
## 我怎么认出来的?
功能对象实例化创建一个带有函数的类实例,就像其他选项一样。不同之处在于所有相关方法也在构造函数中定义。
## 我为什么要用它?
由于为对象的每个实例创建了一组新方法并且可能占用大量内存因此当您知道不打算使用大量实例时功能实例化是有益的。新的和经验丰富的JavaScript编码器也能轻松理解您的代码因为实例化完全是自包含的并且很容易看到方法和对象实例之间的关系。
## 有什么缺点?
功能实例化的缺点是,如果要对代码进行任何更改(例如添加更多方法),则在进行这些更改之前创建的对象的任何实例都不会更新。最终可能会有两个包含不同方法信息的实例。
* * *
## 标题:功能共享
```javascript
var fun = function(a, b) {
var funInstance = {};
funInstance.a = a;
funInstance.b = b;
extend(funInstance, funMethods);
return funInstance;
}
var extend = function(to, from) {
for (var key in from) {
to[key] = from[key];
}
}
var funMethods = {};
funMethods.method1 = function() {
// method code here
}
funMethods.method2 = function() {
// method code here
}
funMethods.method3 = function() {
// method code here
}
var myFun = fun(1, 2);
myFun.method1();
```
## 我怎么认出来的?
Functional和Functional-Shared之间的主要区别在于在Functional-Shared中我们共享我们的方法。我们有一个单独的对象来保存我们的所有方法而不是在我们的Instantiation函数中声明方法。为了使用这些方法我们将它们扩展到正在创建的对象的每个实例中。
## 我为什么要用它?
Functional-Shared允许我们使用对方法的引用而不是为我们对象的每个实例声明和存储我们的方法从而节省了空间。
## 有什么缺点?
缺点是,由于方法是通过指向方法对象的指针引用的,如果我们以任何方式更新方法对象,那么在更改之前创建的对象实例将不会更新。最终可能会有两个对象实例引用两个不同版本的方法。
* * *
## 标题:原型
```javascript
var fun = function(a, b) {
var funInstance = Object.create(funMethods);
funInstance.a = a;
funInstance.b = b;
return funInstance;
}
var funMethods = {};
funMethods.method1 = function() {
// method code here
}
funMethods.method2 = function() {
// method code here
}
funMethods.method3 = function() {
// method code here
}
var myFun = fun(1, 2);
myFun.method1();
```
## 我怎么认出来的?
Prototypal类似于Functional-Shared因为它们都使用单独的方法对象来保存将在我们创建的对象的实例之间共享的所有方法。不同之处在于我们可以使用原型链。我们可以使用Object.createprototype创建对象以便将方法附加到我们的对象实例。持有我们共享方法的对象被认为是原型。
## 我为什么要用它?
如果在创建对象实例后对原型进行更改,则将更新该实例。你不会得到两个具有不同方法的相同原型的实例。
## 有什么缺点?
使用此方法的缺点是需要额外的步骤和额外的代码。我们不仅要像以前一样创建和返回我们的对象,还要装饰它。
* * *
## 标题:伪古典
```javascript
var Fun = function(a, b) {
// this = Object.create(Fun.prototype);
this.a = a;
this.b = b;
// return this;
}
Fun.prototype.method1 = function() {
// method code here
}
Fun.prototype.method2 = function() {
// method code here
}
Fun.prototype.method3 = function() {
// method code here
}
var myFun = new Fun(1, 2);
myFun.method1();
```
## 我怎么认出来的?
伪古典实例化到目前为止包含的代码量最少。 new关键字不是创建新对象并返回它而是为我们做到这一点。在引擎盖下当您使用new关键字实例化对象时使用this = Object.createObject.prototype创建一个新对象其中这指的是以new关键字命名的原型。当我们定义方法时我们使用prototype关键字。
## 我为什么要用它?
Pseudoclassical被认为是最快的实例化模式如果您要创建数万个实例这将非常有用。它也是最优化的因为它利用了Javascript功能。
## 有什么缺点?
Pseudoclassical Instantiation的缺点是它需要更多关于JavaScript正在做什么的知识特别是使用this关键字。这使得这种类型的对象实例化更难以理解特别是如果其他人正在阅读您的代码