面向对象的Javascript - 如何定义类中的类?来自C#的例子 [英] Object Oriented Javascript - How To Define A Class Within A Class? from a C# example
本文介绍了面向对象的Javascript - 如何定义类中的类?来自C#的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道在SO上有很多OO javascript问题,而且我已经阅读了很多资源....但到目前为止它仍然是我最长的学习曲线!
I know there are a lot of OO javascript questions on SO and I a have been reading a lot of resources.... BUT it is still far my most long winded learning curve so far!
我不是经典的训练对不起,因此我必须向你们展示c#我想要实现的一个例子。
I am not classically trained sorry, hence I will have to just show you guys in c# an example of what I want to acheive.
我希望你能帮忙!
public class Engine
{
public int EngineSize;
public Engine()
{
}
}
public class Car
{
public Engine engine;
public Car()
{
engine = new Engine();
}
}
伙计我真的不担心私人/公众&安培;以上C#示例的命名约定。
guys I am not really worried about the private/public & naming conventions of the above C# example.
我想知道的是如何在Javascript中复制这个结构?
All I want to know is how to replicate this structure in Javascript?
谢谢!
推荐答案
function Engine(size) {
var privateVar;
function privateMethod () {
//...
}
this.publicMethod = function () {
// with access to private variables and methods
};
this.engineSize = size; // public 'field'
}
function Car() { // generic car
this.engine = new Engine();
}
function BMW1800 () {
this.engine = new Engine(1800);
}
BMW1800.prototype = new Car(); // inherit from Car
var myCar = new BMW1800();
这篇关于面向对象的Javascript - 如何定义类中的类?来自C#的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文