“摘要”的最佳实践JavaScript 中的函数?
我刚刚编写了一些 JavaScript 代码,这些代码遵循我认为创建带有闭包和一些函数的对象的良好实践:
var myStuff = (function() {
var number = 0;
var fn = {};
fn.increment = function() { number++; };
fn.decrement = function() { number--; };
fn.getNumber = function() { return number; };
return fn;
})();
myStuff.increment();
myStuff.increment();
alert(myStuff.getNumber()); // alerts '2'
我编写像前面的代码片段这样的代码没有问题。我想编写一些具有类似于 OOP“抽象”类功能的代码。这是我努力的结果:
var myStuff = (function () {
var number = 0;
var fn = {};
fn.increment = function () { number++; };
fn.decrement = function () { number--; };
fn.doSomethingCrazy = function () { throw new Error('not implemented'); }; // I want to specify later what this does.
fn.doSomethingCrazyTwice = function () { fn.doSomethingCrazy(); fn.doSomethingCrazy(); };
fn.getNumber = function () { return number; };
return fn;
})();
myStuff.doSomethingCrazy = function () { this.increment(); this.increment(); };
myStuff.doSomethingCrazyTwice();
alert(myStuff.getNumber()); // alerts '4'
上面的代码片段可以工作,但看起来并不优雅。也许我试图强制 JavaScript(一种函数式语言)做一些它不打算做的事情(对象继承)
在 JavaScript 中定义对象以便稍后可以定义该对象的函数的好方法是什么?
I just wrote some JavaScript code that follows along with what I believe to be good practice for creating an object with closure and some functions:
var myStuff = (function() {
var number = 0;
var fn = {};
fn.increment = function() { number++; };
fn.decrement = function() { number--; };
fn.getNumber = function() { return number; };
return fn;
})();
myStuff.increment();
myStuff.increment();
alert(myStuff.getNumber()); // alerts '2'
I have no problem writing code like the previous snippet. I would like to write some code with functionality similar to a OOP "abstract" class. Here is the result of my effort:
var myStuff = (function () {
var number = 0;
var fn = {};
fn.increment = function () { number++; };
fn.decrement = function () { number--; };
fn.doSomethingCrazy = function () { throw new Error('not implemented'); }; // I want to specify later what this does.
fn.doSomethingCrazyTwice = function () { fn.doSomethingCrazy(); fn.doSomethingCrazy(); };
fn.getNumber = function () { return number; };
return fn;
})();
myStuff.doSomethingCrazy = function () { this.increment(); this.increment(); };
myStuff.doSomethingCrazyTwice();
alert(myStuff.getNumber()); // alerts '4'
The above code snippet works, but it doesn't seem graceful. Perhaps I'm trying to force JavaScript (a functional language) to do something it isn't designed to do (object inheritance)
What is a good way to define an object in JavaScript so that a function of that object can be defined later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是不定义该函数。
Javascript 是一种鸭子类型语言。如果它看起来像鸭子并且嘎嘎叫起来像鸭子,那么它就是鸭子。
您不需要做任何特殊的事情来完成这项工作;只要调用时该函数存在,它就可以正常工作。
如果您在没有该函数的实例上调用它,您将在调用站点收到错误。
Just don't define the function.
Javascript is a duck-typed language. If it looks like a duck and it quacks like a duck, it is a duck.
You don't need to do anything special to make this work; as long as the function exists when you call it, it will work fine.
If you call it on an instance that doesn't have the function, you'll get an error at the callsite.
我同意 SLAks,没有必要定义函数,但我还是倾向于这样做。那是因为对我来说重要的部分是在文档中。当有人阅读我的课程时,我希望清楚您必须实现这些方法,将传递哪些参数以及应该返回什么。
这是工作中的一个文件。具有定期加载数据的基类的功能有多种实现。
2019 更新
如果可以在 TypeScript 中声明抽象方法,请使用 TypeScript
I agree with SLaks, there's no need to define the function, but I tend to anyway. That's because to me the important part is in the documentation. When somebody reads my class, I want it to be clear that you must implement these methods, what arguments will be passed and what should be returned.
This is from a file at work. There were multiple implementations of a feature with a base class that did the data loading at intervals.
2019 Update
Use TypeScript if you can Declaring abstract method in TypeScript
随着我们的团队不断壮大,我们的 javascript 项目变得越来越复杂,我们也必须开始实现 OO 功能。
在我们的 javascript 'abstract' 方法中,我们只是抛出一个错误,或者弹出一个警报。这是来自 Page 对象的示例:
在 Java 世界中,它类似于:
Java 代码给出编译时错误,但是在 Javascript 中我们会得到运行时错误 。 (一个脏错误对话框表明实现对象尚未实现该方法)。
我们有许多不同的团队使用 Page 对象; “鸭子打字”的哲学绝对不适合我们。如果没有这些伪“抽象”方法,我们通常会缺乏 API 通信,有时我们会破坏超级对象(即,因为用户不知道他们应该实现该方法)。
我厌倦了这种“鸭子打字”的哲学。我不确定支持者是否曾经参与过拥有 10 多个开发人员的复杂 Javascript 项目。
As our team is growing and our javascript project is getting more complex we have to start implementing OO features as well.
In our javascript 'abstract' method we simply throw an error, or pop up an alert. This is an example from out Page object:
In java world it is analagous to :
The Java code gives a compile time error, however in the Javascript we would get a runtime error. (a dirty error dialog saying that an implementing object hasn't implemented that method yet).
We have a number of disparate teams that use the Page object; the philosophy of 'duck typing' absolutely does not cut it with us. Without these pseudo 'abstract' methods we have a general lack of API communication, and sometimes we get sabotaging of the super object (ie. because a user has no idea they are supposed to implement the method).
I am tired of this 'duck typing' philosophy. I'm not sure if proponents have ever been in a complex Javascript project with 10+ developers.
如果您觉得自己的方式不优雅,可能有一种方法可以创建一些函数来简化流程,使其看起来更好。但回到主题...
是的,Javascript 有内置的委托,也称为继承,通过原型。
给定一个原型对象:
我们可以创建一个继承它的新对象:
请注意,并不总是支持直接通过 Object.create 执行操作(旧浏览器等)。旧的(有些人可能会说,正常)做事的方式是通过时髦的 new 运算符(不要对这个名字想太多 - 它故意令人困惑)分散 Java 人员的注意力)
与原型继承相比,需要考虑的一个重要区别是缺少私有变量。 只有公共变量可以被继承!因此,一个常见的约定是使用下划线作为私有变量和受保护变量的前缀。
If you don't find your way graceful there is probably a way to create some functions to stramline the process to make it look better. But back to the topic...
Yes, Javascript has builtin delegation, aka inheritance, via prototypes.
Given a prototypal object:
We can create a new object that inherits from it:
Just note, that doing things directly via Object.create is not always supported (old browsers, etc). The old (and some may say, normal) way do do stuff is via the funky new operator (don´t think too much on the name - its confusing on purpose to distract the Java people)
An important difference to consider with prototypical inheritance is the lack of private variables. Only public variables can be inherited! Because of this, a common convention is to use underscore as a prefix for private and protected variables.
您可能想看看上一篇文章 如何我要在 JavaScript 中创建一个抽象基类吗?
只是一些网站供您阅读有关 OOP 和 JavaScript 的一些内容,我假设您基于您所说的评论而对 JavaScript 作为 OOP 语言感到陌生
http://mckoss.com/jscript/object.htm
http://www.codeproject.com/KB/aspnet/JsOOP1.aspx
http://www.javascriptkit.com/javatutors/oopjs.shtml
You might want to take a look at this previous post How do I create an abstract base class in JavaScript?
Just a few sites for some light reading for you on OOP and JavaScript, I am assuming that your new to JavaScript as an OOP langauge based of a comment you said
http://mckoss.com/jscript/object.htm
http://www.codeproject.com/KB/aspnet/JsOOP1.aspx
http://www.javascriptkit.com/javatutors/oopjs.shtml