在多态中的超类中使用子类原型中定义的变量(Use a variable defined in sub classes prototype in superclass in polymorphism)

编程入门 行业动态 更新时间:2024-10-22 13:43:30
在多态中的超类中使用子类原型中定义的变量(Use a variable defined in sub classes prototype in superclass in polymorphism)

我构造了一个在Node JS使用多态的代码。当projectModule , testCase和businessComponent是它的子类时,这个item是我的超类。 在我的例子中,除文件读取操作外,每个操作都是相同的。 所以我使用<sub-class>.prototype.create.itemType来创建具有create()函数的唯一新变量。 我打电话的时候

var test = new projectModule(); test.create();

控制台日志文本create functions is called意味着使用item原型成功但代码不正确,因为itemType undefined 。 我如何使用itemType变量?

var item = function () {}; item.prototype.create = function () { console.log('create functions is called'); if(itemType==='module') console.log('read module json'); if(itemType==='tc') console.log('read tc json'); if(itemType==='bc') console.log('read bc json'); } var projectModule = function () {}; projectModule.prototype = Object.create(item.prototype); projectModule.prototype.create.itemType = 'module'; var testCase = function () {}; testCase.prototype = Object.create(item.prototype); testCase.prototype.create.itemType = 'tc'; var businessComponent = function () {}; businessComponent.prototype = Object.create(item.prototype); businessComponent.prototype.create.itemType = 'bc'; //call the function var test = new projectModule(); test.create();

I construct a code to use polymorphism in Node JS.Here item is my super class when the projectModule, testCase and businessComponent are it's sub classes. In my case, every operation is the same except the file reading operation. So I used <sub-class>.prototype.create.itemType to create, unique, new variable withing create() function. When i call

var test = new projectModule(); test.create();

console log the text create functions is called means using item prototype is successful but code doesn't work correct because itemType is undefined. How can I use itemType variable?

var item = function () {}; item.prototype.create = function () { console.log('create functions is called'); if(itemType==='module') console.log('read module json'); if(itemType==='tc') console.log('read tc json'); if(itemType==='bc') console.log('read bc json'); } var projectModule = function () {}; projectModule.prototype = Object.create(item.prototype); projectModule.prototype.create.itemType = 'module'; var testCase = function () {}; testCase.prototype = Object.create(item.prototype); testCase.prototype.create.itemType = 'tc'; var businessComponent = function () {}; businessComponent.prototype = Object.create(item.prototype); businessComponent.prototype.create.itemType = 'bc'; //call the function var test = new projectModule(); test.create();

最满意答案

看起来您需要有关原型继承如何在javascript中工作的信息。

您要找的是关键字this 。

从这里拉出示例代码

function Plant () { ​this.country = "Mexico"; ​this.isOrganic = true; } ​ ​// Add the showNameAndColor method to the Plant prototype property​ Plant.prototype.showNameAndColor = function () { console.log("I am a " + this.name + " and my color is " + this.color); } ​ ​// Add the amIOrganic method to the Plant prototype property​ Plant.prototype.amIOrganic = function () { ​if (this.isOrganic) console.log("I am organic, Baby!"); } ​ ​function Fruit (fruitName, fruitColor) { ​this.name = fruitName; ​this.color = fruitColor; } ​ ​// Set the Fruit's prototype to Plant's constructor, thus inheriting all of Plant.prototype methods and properties.​ Fruit.prototype = new Plant (); ​ ​// Creates a new object, aBanana, with the Fruit constructor​ ​var aBanana = new Fruit ("Banana", "Yellow"); ​ ​// Here, aBanana uses the name property from the aBanana object prototype, which is Fruit.prototype:​ console.log(aBanana.name); // Banana​ ​ ​// Uses the showNameAndColor method from the Fruit object prototype, which is Plant.prototype. The aBanana object inherits all the properties and methods from both the Plant and Fruit functions.​ console.log(aBanana.showNameAndColor()); // I am a Banana and my color is yellow.

Looks like you are in need of information on how prototypical inheritance works in javascript.

What you are looking for is the keyword this.

Example code pulled from here

function Plant () { ​this.country = "Mexico"; ​this.isOrganic = true; } ​ ​// Add the showNameAndColor method to the Plant prototype property​ Plant.prototype.showNameAndColor = function () { console.log("I am a " + this.name + " and my color is " + this.color); } ​ ​// Add the amIOrganic method to the Plant prototype property​ Plant.prototype.amIOrganic = function () { ​if (this.isOrganic) console.log("I am organic, Baby!"); } ​ ​function Fruit (fruitName, fruitColor) { ​this.name = fruitName; ​this.color = fruitColor; } ​ ​// Set the Fruit's prototype to Plant's constructor, thus inheriting all of Plant.prototype methods and properties.​ Fruit.prototype = new Plant (); ​ ​// Creates a new object, aBanana, with the Fruit constructor​ ​var aBanana = new Fruit ("Banana", "Yellow"); ​ ​// Here, aBanana uses the name property from the aBanana object prototype, which is Fruit.prototype:​ console.log(aBanana.name); // Banana​ ​ ​// Uses the showNameAndColor method from the Fruit object prototype, which is Plant.prototype. The aBanana object inherits all the properties and methods from both the Plant and Fruit functions.​ console.log(aBanana.showNameAndColor()); // I am a Banana and my color is yellow.

更多推荐

本文发布于:2023-08-07 06:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   原型   变量   类中   定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!