Javascript OO语法 [英] Javascript OO syntax

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

问题描述

似乎有许多不同的方式在JavaScript中执行OO。

There seem to be many different ways of doing OO in JavaScript.

我喜欢:

function ClassA(){};
ClassA.prototype={
    someFunc:function(a,b,c){},
    otherFunc:function(){}
}
var c=new ClassA();

并且从未使用超出此功能的功能(尽管是精通OOer)。我怀疑这是老式的,因为我经常看到新的闪烁变体,这让我想知道我是否选择了最好的方法。例如,你可以在构造函数方法中创建魔法来创建私有变量和访问器方法,我认为(直到最近)是不可能的。子类化怎么样?我不知道如何实现这一点,但它现在必须有某种共同的模式。

and have never used features beyond what this provides (despite being a proficient OOer). I suspect this is old fashioned, because every so often I see new spangled variants, and it makes me wonder if I'm choosing the best approach. For instance, you can do magic in the constructor method to create private variables and accessor methods, something I believed (until relatively recently) to be impossible. What about subclassing? I wouldn't know how to achieve this, but it must have some sort of common pattern by now.

你是如何做到的?为什么?

How do you do it and why?

推荐答案

function foo() {
  var bar = function() { console.log("i'm a private method"); return 1; };
  var iAmAPrivateVariable = 1;

  return {
    publicMethod: function() { alert(iAmAPrivateVariable); },
    publicVariable: bar()
  }
}

//usage
var thing = foo()

这被称为功能性应用程序,因为您实际上正在利用闭包进行封装(这是在javascript中执行此操作的唯一方法)。

This is known as a functional appoach, since you are actually leveraging closures for encapsulation (which is the only way to do it in javascript).

一般来说,你不应该在javascript中做OO,因为很多原因它不是那么好的语言。认为方案有波浪括号和分号,你将开始像专业人士那样编写语言。话虽如此,有时OO更合适。在这些情况下,上述通常是最好的选择

In a general way, you shouldn't be doing OO in javascript, it isn't that great a language for it for a great many reasons. Think scheme with squiggly brackets and semi-colons, and you will start writing the language like the pros do. That being said, sometime OO is a better fit. In those cases, the above is typically the best bet

编辑:将继承带入混合

function parent() {
  return { parentVariable: 2 };
}

function foo() {
  var bar = function() { console.log("i'm a private method"); return 1; };
  var iAmAPrivateVariable = 1;

  me = parent();
  me.publicMethod = function() { alert(iAmAPrivateVariable); };
  me.publicVariable = bar();

  return me;
}

这会让事情变得更加复杂,但仍会达到预期的最终结果对OO概念采用函数式方法(在这种情况下,使用装饰函数而不是实际继承)。我对整个方法的喜欢之处在于我们仍然按照这种语言的方式处理对象 - 可以随意附加内容的属性包。

This makes things a tad more complected, but accomplishes the desired end result while still taking a functional approach to OO concepts (in this case, using decorator functions instead of real inheritance). What I like about the whole approach is we are still really treating objects the way they are intended to be in this kind of language -- a property bag you can attach stuff to at will.

EDIT2:

只想在信用到期时给予信用,这种方法对于doug crockford在Javascript:The Good Parts中的建议略有简化。如果你想把你的js技能提升到一个新的水平,我强烈建议从那里开始。我不认为我从这么小的书中学到了这么多。

Just wanted to give credit where credit is due, this approach is a very slight simplification to what doug crockford suggests in Javascript: The Good Parts. If you want to take your js skills to the next level, I would highly suggest starting there. I don't think I have ever learned so much from such a small book.

另一个注意事项是,这与大多数时候你会看到的大不相同您将要工作的工作,往往很难解释a)发生了什么,以及b)为什么对同事来说这是个好主意。

Another note is this is wildly different then what you will see most of the time in most of the jobs you will ever work at, and often is very hard to explain a) what is going on, and b) why it is a good idea to coworkers.

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

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