Javascript 使用KendoUI进行继承-调用基方法
是否可以使用kendojavascript继承调用base方法? 我试试这个:Javascript 使用KendoUI进行继承-调用基方法,javascript,kendo-ui,Javascript,Kendo Ui,是否可以使用kendojavascript继承调用base方法? 我试试这个: function log(msg) { $("#content").append("<p>" + msg + "</p>"); } var Person = kendo.Class.extend({ firstName: 'Not Set', lastName: 'Not Set', isPrettyCoolPerson: false, init: f
function log(msg) {
$("#content").append("<p>" + msg + "</p>");
}
var Person = kendo.Class.extend({
firstName: 'Not Set',
lastName: 'Not Set',
isPrettyCoolPerson: false,
init: function(firstName, lastName) {
if (firstName) this.firstName = firstName;
if (lastName) this.lastName = lastName;
},
sayHello: function() {
log("Hello! I'm " + this.firstName + " " + this.lastName)
}
});
var Parent = Person.extend({
firstName: 'Mark',
lastName: 'Holland',
sayHello: function() {
Person.sayHello.call(this);
log("Hello from Parent! I'm " + this.firstName + " " + this.lastName)
}
});
var myDad = new Parent();
myDad.sayHello();
函数日志(msg){
$(“#内容”)。追加(“”+msg+””);
}
var Person=kendo.Class.extend({
firstName:'未设置',
lastName:'未设置',
isPrettyCoolPerson:false,
init:函数(firstName,lastName){
如果(firstName)this.firstName=firstName;
如果(lastName)this.lastName=lastName;
},
sayHello:function(){
日志(“你好!我是”+this.firstName+“”+this.lastName)
}
});
var Parent=Person.extend({
名字:“马克”,
姓:“荷兰”,
sayHello:function(){
Person.sayHello.call(这个);
日志(“家长您好!我是”+this.firstName+“”+this.lastName)
}
});
var myDad=新父级();
我爸爸,你好;
这是:
function log(msg) {
$("#content").append("<p>" + msg + "</p>");
}
var Person = kendo.Class.extend({
firstName: 'Not Set',
lastName: 'Not Set',
isPrettyCoolPerson: false,
init: function(firstName, lastName) {
if (firstName) this.firstName = firstName;
if (lastName) this.lastName = lastName;
},
sayHello: function() {
log("Hello! I'm " + this.firstName + " " + this.lastName)
}
});
var Parent = Person.extend({
firstName: 'Mark',
lastName: 'Holland',
sayHello: function() {
Person.call(this);
log("Hello from Parent! I'm " + this.firstName + " " + this.lastName)
}
});
var myDad = new Parent();
myDad.sayHello();
函数日志(msg){
$(“#内容”)。追加(“”+msg+””);
}
var Person=kendo.Class.extend({
firstName:'未设置',
lastName:'未设置',
isPrettyCoolPerson:false,
init:函数(firstName,lastName){
如果(firstName)this.firstName=firstName;
如果(lastName)this.lastName=lastName;
},
sayHello:function(){
日志(“你好!我是”+this.firstName+“”+this.lastName)
}
});
var Parent=Person.extend({
名字:“马克”,
姓:“荷兰”,
sayHello:function(){
打电话给(这个);
日志(“家长您好!我是”+this.firstName+“”+this.lastName)
}
});
var myDad=新父级();
我爸爸,你好;
唯一的区别是人。打电话(这个);和人打招呼。打电话给(这个);。
但无论哪种情况,它都不起作用。这些方法被添加到Person函数的原型中,因此您需要更改此调用:
Person.sayHello.call(this);
到
或(同等)
我喜欢使用KendoUI处理继承,使用一个变量存储基本“类”,另一个变量存储定义的“类”。我在一个立即调用的内联函数中声明这两个变量,这样它们的作用域就不会超出定义代码。这样做,如果基“类”更改,代码只需在1个位置更改:
var Person = (function () {
var $base = kendo.Class;
var $class = $base.extend({
firstName: null,
lastName: null,
isPrettyCoolPerson: null,
init: function (firstName, lastName) {
if (firstName) this.firstName = firstName;
if (lastName) this.lastName = lastName;
},
sayHello: function () {
console.log("Hello! I'm " + this.firstName + " " + this.lastName);
}
});
return $class;
})();
var Parent = (function () {
var $base = Person;
var $class = $base.extend({
sayHello: function () {
$base.fn.sayHello.call(this);
console.log("...and I'm a parent!");
}
});
return $class;
})();
var person = new Person("Steve", "Gates");
person.sayHello();
var parent = new Parent("Bill", "Jobs");
parent.sayHello();
定义模型时也可以使用此方法:
var Shape = (function () {
var $base = kendo.data.Model;
var $class = $base.define({
id: "Id",
fields: {
Id: { type: "number" },
Name: { nullable: true },
Area: { type: "number" }
}
});
return $class;
})();
var Rectangle = (function () {
var $base = Shape;
var $class = $base.define({
id: "Id",
fields: {
Width: { type: "number" },
Height: { type: "number" }
}
});
return $class;
})();
var shape = new Shape({ Id: 1, Name: "Amorphous Blob", Area: Math.E * Math.PI });
console.log(shape.Area);
var rectangle = new Rectangle({ Id: 2, Name: "Henry the Rectangle", Width: 3, Height: 4 });
rectangle.Area = rectangle.Width * rectangle.Height;
console.log(rectangle.Area);
这仅仅意味着要自己实现继承,而不是实际使用Kendo的继承实现。通常,调用父方法是经典继承中的一个基本问题。因此,任何声称实现了经典继承的库都必须实现它。此外,为每个类定义所有这些变量意味着大量代码重复。@这个答案如何满足需要被否决的条件?我以为它是在重新实现已经实现的特性,但我忽略了不必多次编写父类名的要点。我应该更仔细地读它。很抱歉。值得注意的是,如果函数有参数,那么您只需将参数放在
后面。比如Person.fn.sayHello.call(这个,名字,姓氏)代码>,如果sayHello有两个参数。
var Person = (function () {
var $base = kendo.Class;
var $class = $base.extend({
firstName: null,
lastName: null,
isPrettyCoolPerson: null,
init: function (firstName, lastName) {
if (firstName) this.firstName = firstName;
if (lastName) this.lastName = lastName;
},
sayHello: function () {
console.log("Hello! I'm " + this.firstName + " " + this.lastName);
}
});
return $class;
})();
var Parent = (function () {
var $base = Person;
var $class = $base.extend({
sayHello: function () {
$base.fn.sayHello.call(this);
console.log("...and I'm a parent!");
}
});
return $class;
})();
var person = new Person("Steve", "Gates");
person.sayHello();
var parent = new Parent("Bill", "Jobs");
parent.sayHello();
var Shape = (function () {
var $base = kendo.data.Model;
var $class = $base.define({
id: "Id",
fields: {
Id: { type: "number" },
Name: { nullable: true },
Area: { type: "number" }
}
});
return $class;
})();
var Rectangle = (function () {
var $base = Shape;
var $class = $base.define({
id: "Id",
fields: {
Width: { type: "number" },
Height: { type: "number" }
}
});
return $class;
})();
var shape = new Shape({ Id: 1, Name: "Amorphous Blob", Area: Math.E * Math.PI });
console.log(shape.Area);
var rectangle = new Rectangle({ Id: 2, Name: "Henry the Rectangle", Width: 3, Height: 4 });
rectangle.Area = rectangle.Width * rectangle.Height;
console.log(rectangle.Area);