你是如何用Javascript编写一个类的? [重复]

时间:2022-04-16 06:36:59

Possible Duplicate:
What's the best way to define a class in javascript

可能重复:在javascript中定义类的最佳方法是什么

How do you write a class in Javascript? Is it even possible?

你是如何用Javascript编写一个类的?它甚至可能吗?

6 个解决方案

#1


Well, JavaScript is a Prototype-Based language, it does not have classes, but you can have classical inheritance, and other behavior reuse patterns through object cloning and prototyping.

嗯,JavaScript是一种基于Prototype的语言,它没有类,但您可以拥有经典继承,其他行为通过对象克隆和原型设计重用模式。

Recommended articles:

#2


function Foo() {
   // constructor
}

Foo.prototype.bar = function() {
    // bar function within foo
}

#3


Javascript uses prototype-based OO by default.

Javascript默认使用基于原型的OO。

However, if you're using prototype library, for example, you can use Class.create().

但是,如果您正在使用原型库,则可以使用Class.create()。

http://prototypejs.org/api/class/create

It would let you to create (or inherit) a class, after that you would instantiate its instances with new.

它将允许您创建(或继承)一个类,之后您将使用new实例化其实例。

Other libraries probably have similar facilities.

其他图书馆可能有类似的设施。

#4


If you use a library like prototype or jQuery its a lot easier but the legacy way is to do this.

如果你使用像prototype或jQuery这样的库它会更容易,但传统方法是这样做。

function MyClass(){
}
MyClass.prototype.aFunction(){ 
}

var instance = new MyClass();
instance.aFunction();

You can read more on it here http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/

你可以在这里阅读更多内容http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/

#5


It's "sort of" possible. Prototype has some built-in help with writing classes in JS. Take a look at this thorough description of how you can do it.

它“有点”可能。 Prototype在JS中编写类有一些内置的帮助。看看如何做到这一点的详尽描述。

var namedClass = Class.create({
    initialize: function(name) {
        this.name = name;
    }

    getName: function() {
        return this.name;
    }
});
var instance = new namedClass('Foobar');

#6


JavaScript is based on objects, not classes. It uses prototypal inheritance, not classical inheritance.

JavaScript基于对象,而不是类。它使用原型继承,而不是经典继承。

JavaScript is malleable and easily extensible. As a result, there are many libraries that add classical inhertance to JavaScript. However, by using them you risk writing code that's difficult for most JavaScript programmers to follow.

JavaScript具有可塑性且易于扩展。因此,有许多库为JavaScript添加了经典的插入。但是,通过使用它们,您可能会编写大多数JavaScript程序员难以遵循的代码。

#1


Well, JavaScript is a Prototype-Based language, it does not have classes, but you can have classical inheritance, and other behavior reuse patterns through object cloning and prototyping.

嗯,JavaScript是一种基于Prototype的语言,它没有类,但您可以拥有经典继承,其他行为通过对象克隆和原型设计重用模式。

Recommended articles:

#2


function Foo() {
   // constructor
}

Foo.prototype.bar = function() {
    // bar function within foo
}

#3


Javascript uses prototype-based OO by default.

Javascript默认使用基于原型的OO。

However, if you're using prototype library, for example, you can use Class.create().

但是,如果您正在使用原型库,则可以使用Class.create()。

http://prototypejs.org/api/class/create

It would let you to create (or inherit) a class, after that you would instantiate its instances with new.

它将允许您创建(或继承)一个类,之后您将使用new实例化其实例。

Other libraries probably have similar facilities.

其他图书馆可能有类似的设施。

#4


If you use a library like prototype or jQuery its a lot easier but the legacy way is to do this.

如果你使用像prototype或jQuery这样的库它会更容易,但传统方法是这样做。

function MyClass(){
}
MyClass.prototype.aFunction(){ 
}

var instance = new MyClass();
instance.aFunction();

You can read more on it here http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/

你可以在这里阅读更多内容http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/

#5


It's "sort of" possible. Prototype has some built-in help with writing classes in JS. Take a look at this thorough description of how you can do it.

它“有点”可能。 Prototype在JS中编写类有一些内置的帮助。看看如何做到这一点的详尽描述。

var namedClass = Class.create({
    initialize: function(name) {
        this.name = name;
    }

    getName: function() {
        return this.name;
    }
});
var instance = new namedClass('Foobar');

#6


JavaScript is based on objects, not classes. It uses prototypal inheritance, not classical inheritance.

JavaScript基于对象,而不是类。它使用原型继承,而不是经典继承。

JavaScript is malleable and easily extensible. As a result, there are many libraries that add classical inhertance to JavaScript. However, by using them you risk writing code that's difficult for most JavaScript programmers to follow.

JavaScript具有可塑性且易于扩展。因此,有许多库为JavaScript添加了经典的插入。但是,通过使用它们,您可能会编写大多数JavaScript程序员难以遵循的代码。