首次尝试Javascript对象&;继承:对吗
我正在尝试学习如何在Javascript中创建类&如何执行对象继承。我已经学习了一些教程,但我不确定我的代码是否正确首次尝试Javascript对象&;继承:对吗,javascript,class,inheritance,Javascript,Class,Inheritance,我正在尝试学习如何在Javascript中创建类&如何执行对象继承。我已经学习了一些教程,但我不确定我的代码是否正确 我是否正确创建了公共函数和属性?如果没有,我应该改变什么 我是否正确创建了特权函数和属性?如果没有,我应该改变什么 我是否正确创建了私有函数和属性?如果没有,我应该改变什么 我是否正确重写了函数 我是否正确执行了继承 如果有什么问题,你能告诉我代码应该是怎样的吗 下面是创建基类然后创建子类的简单代码: /* Base Object Class */ function Base
- 我是否正确创建了公共函数和属性?如果没有,我应该改变什么李>
- 我是否正确创建了特权函数和属性?如果没有,我应该改变什么李>
- 我是否正确创建了私有函数和属性?如果没有,我应该改变什么李>
- 我是否正确重写了函数李>
- 我是否正确执行了继承李>
- 如果有什么问题,你能告诉我代码应该是怎样的吗
/* Base Object Class */
function BaseClass( /*string*/ objType )
{
/* Public: */
this.name = "blah";
BaseClass.prototype.getName = function()
{
return this.name;
}
BaseClass.prototype.setName = function( newName )
{
var oldName = this.name;
this.name = newName;
return oldName;
}
/* Private: */
var attributeMap = {};
this.constructor = function()
{
// this objects default constructor. Is this correct?
attributeMap["type"] = objType;
attributeMap["uniqueID"] = "Base"+(++INSTANCE_COUNT);
}
/* Privileged: */
// Will an object that inherits from this class be able to override the following functions?
// Or do I have to make these functions public in order to override them?
this.toString = function()
{
var s = "";
for (var attrib in attributeMap)
{
s += attrib + ": " + attributeMap[attrib] + ", ";
}
return s;
}
this.getType = function()
{
return attributeMap["type"];
}
this.renderObject = function()
{
// TODO: render object on HTML5 canvas
}
this.parseXMLNode = function( /*XML Node*/ nodeXML, /*string*/ objType )
{
var attribs = nodeXML.attributes;
for (var i=0; i<attribs.length; i++)
{
attributeMap[ attribs[i].nodeName ] = attribs[i].nodeValue;
}
// store children
if ( nodeXML.hasChildNodes() )
{
attributeMap["children"] = nodeXML.childNodes;
}
reformatObjectInnerHTML();
}
}
// Static Variables //
BaseObject.INSTANCE_COUNT = 0;
// My Child Class //
ChildClass.prototype = new BaseObject( objType ); // make ChildClass inherit from BaseClass
ChildClass.prototype.constructor = function(ObjType) // Make the ChildClass call the BaseClass constructor
{
BaseObject.prototype.constructor.call(this, objType);
}
function ChildClass( /*string*/ objType )
{
/* Privileged: */
// Attempt to override BaseClass function renderObject()
this.renderObject = function()
{
alert("ChildClass::renderObject();");
// Does this override the BaseClass renderObject() function?
}
}
/*基本对象类*/
函数基类(/*string*/objType)
{
/*公众:*/
this.name=“blah”;
BaseClass.prototype.getName=函数()
{
返回此.name;
}
BaseClass.prototype.setName=函数(newName)
{
var oldName=this.name;
this.name=newName;
返回oldName;
}
/*私人:*/
var attributeMap={};
this.constructor=函数()
{
//此对象为默认构造函数。是否正确?
属性映射[“类型”]=对象类型;
属性映射[“uniqueID”]=“Base”+(++实例计数);
}
/*特权:*/
//从该类继承的对象是否能够重写以下函数?
//还是我必须公开这些函数才能覆盖它们?
this.toString=函数()
{
var s=“”;
for(属性映射中的var attrib)
{
s+=attrib+“:“+attributeMap[attrib]+”,”;
}
返回s;
}
this.getType=函数()
{
返回属性映射[“类型”];
}
this.renderObject=函数()
{
//TODO:在HTML5画布上渲染对象
}
this.parseXMLNode=函数(/*XML Node*/nodeXML,/*string*/objType)
{
var attribs=nodeXML.attributes;
对于(var i=0;i
虽然我不建议你编写代码,JavaScript和C++不同,不要在JavaScript中编写C++代码。 在JS中严重继承是不太有用的。如果你需要它们,就把它们复制到另一个对象,只是在那个对象的上下文中调用它们。我在java的土地上做了大量的OOP,但是继承可以在javascript中,通过使用上下文和回调很容易避免。也就是说,当您认为需要继承层次结构时,您可能只需要回调或在不同的上下文中调用函数
但要回答您的问题,这不是“正确”的方法,请查看javascript g
你知道JavaScript是一种原型语言,而不是一种OO语言吗?是的,我知道,我来自C++。虽然我知道它是一种原型语言。虽然我知道它是一种原型语言。如果你有时间,你喜欢探索语言,试着学习IO语言来把你的头环绕在原型语言周围。@ RADAGAISUS这两个AR。e不是相互排斥的。JavaScript当然是一种OO语言。事实上,JavaScript是一种基于原型的、命令式的、功能性的、弱类型的、面向对象的、动态的脚本语言。:P
@ŠimeVidas请参阅和相关引用:“用您正在编写的语言编写”-CrockfordBar.prototype=new Foo();
也不是很好。它限制性太强。例如,您不能将传递给Bar
构造函数的参数传递给Foo
构造函数。
function Foo() {
this.value = 42;
}
Foo.prototype = {
method: function() {}
};
function Bar() {}
// Set Bar's prototype to a new instance of Foo
Bar.prototype = new Foo();
Bar.prototype.foo = 'Hello World';
// Make sure to list Bar as the actual constructor
Bar.prototype.constructor = Bar;
var test = new Bar() // create a new bar instance