Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

javascript - 可以使用哪些技术在JavaScript中定义类,它们的权衡是什么?(What techniques can be used to define a class in JavaScript, and what are their trade-offs?)

I prefer to use OOP in large scale projects like the one I'm working on right now.

(我喜欢在目前正在从事的大型项目中使用OOP。)

I need to create several classes in JavaScript but, if I'm not mistaken, there are at least a couple of ways to go about doing that.

(我需要用JavaScript创建几个类,但是,如果我没记错的话,至少有两种方法可以做到这一点。)

What would be the syntax and why would it be done in that way?

(语法是什么,为什么要用这种方式呢?)

I would like to avoid using third-party libraries - at least at first.

(我想避免使用第三方库-至少在一开始。)


Looking for other answers, I found the article Object-Oriented Programming with JavaScript, Part I: Inheritance - Doc JavaScript that discusses object-oriented programming in JavaScript.

(在寻找其他答案时,我找到了文章“ 使用JavaScript进行面向对象的编程”,第I部分:继承-文档JavaScript ,该文章讨论了JavaScript中的面向对象的编程。)

Is there a better way to do inheritance?

(有更好的继承方法吗?)

  ask by Karim translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's the way to do it without using any external libraries:

(这是无需使用任何外部库即可完成的方法:)

// Define a class like this
function Person(name, gender){

   // Add object properties like this
   this.name = name;
   this.gender = gender;
}

// Add methods like this.  All Person objects will be able to invoke this
Person.prototype.speak = function(){
    alert("Howdy, my name is" + this.name);
};

// Instantiate new objects with 'new'
var person = new Person("Bob", "M");

// Invoke methods like this
person.speak(); // alerts "Howdy, my name is Bob"

Now the real answer is a whole lot more complex than that.

(现在,真正的答案要复杂得多。)

For instance, there is no such thing as classes in JavaScript.

(例如,JavaScript中没有类。)

JavaScript uses a prototype -based inheritance scheme.

(JavaScript使用基于prototype的继承方案。)

In addition, there are numerous popular JavaScript libraries that have their own style of approximating class-like functionality in JavaScript.

(此外,还有许多流行的JavaScript库,它们具有自己的样式,它们近似于JavaScript中的类功能。)

You'll want to check out at least Prototype and jQuery .

(您将至少要签出PrototypejQuery 。)

Deciding which of these is the "best" is a great way to start a holy war on Stack Overflow.

(确定其中哪一个是“最佳”是在Stack Overflow上展开一场圣战的好方法。)

If you're embarking on a larger JavaScript-heavy project, it's definitely worth learning a popular library and doing it their way.

(如果您正着手进行一个较大的JavaScript大型项目,那么绝对值得学习流行的库并按自己的方式去做。)

I'm a Prototype guy, but Stack Overflow seems to lean towards jQuery.

(我是一个原型人,但是Stack Overflow似乎倾向于jQuery。)

As far as there being only "one way to do it", without any dependencies on external libraries, the way I wrote is pretty much it.

(就只有“一种方法”,而没有对外部库的任何依赖,我编写的方法就差不多了。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...