Backbone JS强烈建议您使用jQuery.但是,它没有做jQuery的事情.例如,jQuery消除了新运算符的必要性,骨干网大量使用它.
另一方面,我正在寻找一个更基于原型继承而不是经典继承(新)的框架.jQuery不属于这一类,这只是我倾向于的架构风格.
是否有任何使用原型继承的框架,或者它是否会推出自己的桥模式?
我正在用JavaScript构建一个纸牌游戏,以增加我的网络编程排序,我遇到了JavaScript Prototype继承问题.我的设计有一个基本卡类,包含任何卡需要的所有功能和数据.设计本身相对灵活,因此只需通过更改存储的数据,大约25%的卡可以使用基类.我需要做的是创建从Card继承所有内容的新类 - 包括数据 - 但是在不触及基类函数的情况下覆盖可用函数的一小部分.
我一直在尝试使用原型继承来实现这一目标,但是这会改变基类,它不仅会使用Card类,而且还会从基类继承所有其他函数.
我需要的是一种设计模式,它允许我仅对从Card继承的类重写函数.这在JavaScript中可行吗?
编辑...
对不起,这是一个例子,我们应该首先添加这个.
从基卡类开始.
function Card(){
this.cardID = 0;
this.name = '';
this.imageID = 0;
this.imageURL = '';
this.imageAlt = '';
etc....
}
Card.prototype.init = function( inID
, inName
, inImageID
, inImageURL
, inImageAlt
, inImageHorizontal
etc...
){
this.cardID = inID;
this.name = inName;
this.imageID = inImageID;
this.imageURL = inImageURL;
this.imageAlt = inImageAlt;
}
Card.prototype.whenPlayed = function(){
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在我的孩子班:
ChildCard.prototype = new Card();
ChildCard.constructor = ChildCard;
function ChildCard(){};
ChildCard.prototype.whenPlayed = …Run Code Online (Sandbox Code Playgroud) 这里有一些例子.
// case 1:
var obj1 = {msg : 'Hello'};
var obj2 = obj1;
obj2.msg = "Hi!"; //overwrites
alert(obj1.msg); //=>'Hi!'
// case 2:
var obj1 = {msg : 'Hello'};
var obj2 = Object.create(obj1);
obj2.msg = "Hi!"; //does not overwrite
alert(obj1.msg); //=>'Hello'
// case 3:
var obj1 = {data: { msg : 'Hello'}}
var obj2 = Object.create(obj1);
obj2.data.msg = "Hi!"; //overwrites, Why?
alert(obj1.data.msg); //=>'Hi!'
Run Code Online (Sandbox Code Playgroud)
我认为Object.create()只是让两者都指向相同的原型,而赋值使得两个对象指向相同的位置(不仅仅是原型).
但那么为什么在案例3中数据对象被覆盖?
我正在一个名为Animation.js的文件中创建一个"类":
function Animation(s) {
this.span = s;
};
Animation.prototype = Object.create(Animation.prototype);
Animation.prototype.constructor = Animation;
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为LinearAnimation.js的子类:
function LinearAnimation(s, cP) {
Animation.call(s);
this.controlPoints = cP;
};
LinearAnimation.prototype = Object.create(Animation.prototype);
LinearAnimation.prototype.constructor = LinearAnimation;
Run Code Online (Sandbox Code Playgroud)
问题是,当我访问this.spanLinearAnimation类中的成员时,它说它是undefined.我实施得好吗?谢谢.
javascript constructor prototype class prototypal-inheritance
我有以下代码,我试图从基类继承.为什么代码说identify()没有定义?它不应该从基类调用函数吗?
错误:ReferenceError:识别未定义source1.js:23:9
class TestBase {
constructor() {
this.type = "TestBase";
}
run() {
console.log("TestBase Run");
}
identify() {
console.log("Identify:" + this.type);
}
}
class DerivedBase extends TestBase {
constructor() {
super();
this.type = "DerivedBase";
}
run() {
console.log("DerivedBase Run");
identify();
}
}
window.onload = function() {
let derived = new DerivedBase();
derived.run();
}
Run Code Online (Sandbox Code Playgroud) javascript extends class prototypal-inheritance ecmascript-6
javascript ×4
class ×2
backbone.js ×1
constructor ×1
ecmascript-5 ×1
ecmascript-6 ×1
extends ×1
prototype ×1