我更喜欢在大型项目中使用OOP,比如我现在正在做的项目。我需要在JavaScript中创建几个类,但是,如果我没有弄错的话,至少有几种方法可以做到这一点。它的语法是什么,为什么要这样做?
我希望避免使用第三方库——至少一开始是这样。 为了寻找其他答案,我找到了一篇文章《JavaScript面向对象编程,第一部分:继承- JavaScript文档》,它讨论了JavaScript中的面向对象编程。是否有更好的继承方式?
我更喜欢在大型项目中使用OOP,比如我现在正在做的项目。我需要在JavaScript中创建几个类,但是,如果我没有弄错的话,至少有几种方法可以做到这一点。它的语法是什么,为什么要这样做?
我希望避免使用第三方库——至少一开始是这样。 为了寻找其他答案,我找到了一篇文章《JavaScript面向对象编程,第一部分:继承- JavaScript文档》,它讨论了JavaScript中的面向对象编程。是否有更好的继承方式?
当前回答
//新方法使用this和new 人员(姓名){ This.name = name; 这一点。Greeting = function() { 警报('你好!I\'m ' + this.name + '.'); }; } var gee=新人员(“gee”); gee.greeting (); var gray=新人员(“灰色”); gray.greeting (); / /老方法 函数createPerson(名字){ var obj = {}; obj.name =名称; obj。Greeting = function(){ console.log("hello I am"+obj.name); }; 返回obj; } var吉塔= createPerson(“吉塔”); gita.greeting ();
其他回答
在JavaScript中定义类的最好方法是不定义类。
认真对待。
面向对象有几种不同的风格,其中一些是:
基于类的OO(首先由Smalltalk引入) 基于原型的OO(由Self首次引入) 基于多方法的面向对象(我认为是由CommonLoops首次引入的) 基于谓词的OO(不知道)
可能还有一些我不知道的。
JavaScript实现了基于原型的OO。在基于原型的OO中,通过复制其他对象来创建新对象(而不是从类模板实例化),方法直接存在于对象中而不是类中。继承是通过委托完成的:如果一个对象没有方法或属性,它会在它的原型(即它被克隆的对象)中查找,然后是原型的原型,以此类推。
换句话说:没有阶级。
JavaScript actually has a nice tweak of that model: constructors. Not only can you create objects by copying existing ones, you can also construct them "out of thin air", so to speak. If you call a function with the new keyword, that function becomes a constructor and the this keyword will not point to the current object but instead to a newly created "empty" one. So, you can configure an object any way you like. In that way, JavaScript constructors can take on one of the roles of classes in traditional class-based OO: serving as a template or blueprint for new objects.
JavaScript是一种非常强大的语言,因此如果您愿意,可以很容易地在JavaScript中实现基于类的OO系统。但是,只有在确实需要时才应该这样做,而不是仅仅因为Java就是这样做的。
基于tritych的例子,这可能更简单:
// Define a class and instantiate it
var ThePerson = new function Person(name, gender) {
// Add class data members
this.name = name;
this.gender = gender;
// Add class methods
this.hello = function () { alert('Hello, this is ' + this.name); }
}("Bob", "M"); // this instantiates the 'new' object
// Use the object
ThePerson.hello(); // alerts "Hello, this is Bob"
这只创建了一个对象实例,但如果你想在一个类中封装一堆变量和方法的名称,这仍然是有用的。通常构造函数中不会有“Bob, M”参数,例如,如果方法将调用具有自己数据的系统,例如数据库或网络。
我仍然太新与JS看到为什么这不使用原型的东西。
我认为你应该阅读Douglas Crockford的《JavaScript中的原型继承》和《JavaScript中的经典继承》。
来自他的页面的例子:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
效果吗?它将允许你以更优雅的方式添加方法:
function Parenizor(value) {
this.setValue(value);
}
Parenizor.method('setValue', function (value) {
this.value = value;
return this;
});
我也推荐他的视频: 先进的JavaScript。
你可以在他的个人主页http://javascript.crockford.com/上找到更多视频 在John Reisig的书中,你可以从Douglas Crockfor的网站上找到许多例子。
简单的方法是:
function Foo(a) {
var that=this;
function privateMethod() { .. }
// public methods
that.add = function(b) {
return a + b;
};
that.avg = function(b) {
return that.add(b) / 2; // calling another public method
};
}
var x = new Foo(10);
alert(x.add(2)); // 12
alert(x.avg(20)); // 15
这样做的原因是,如果您将一个方法作为事件处理程序提供,那么它可以绑定到其他东西,因此您可以在实例化期间保存该值,并在以后使用它。
编辑:这绝对不是最好的方法,只是一种简单的方法。我也在等待好的答案!
//新方法使用this和new 人员(姓名){ This.name = name; 这一点。Greeting = function() { 警报('你好!I\'m ' + this.name + '.'); }; } var gee=新人员(“gee”); gee.greeting (); var gray=新人员(“灰色”); gray.greeting (); / /老方法 函数createPerson(名字){ var obj = {}; obj.name =名称; obj。Greeting = function(){ console.log("hello I am"+obj.name); }; 返回obj; } var吉塔= createPerson(“吉塔”); gita.greeting ();