JavaScript:从超类实例内部创建子类
我对 Javascript 编码非常有经验,但仍有一件事我无法真正理解。
我有一个超类,比方说类别。现在我想从 Category 实例内部创建子类的一些实例,比如 Post。我希望 Post 拥有自己的属性,但它还需要能够访问其父级的属性。这就是这个概念:
/* Superclass */
function Category(catID) {
this.catID = catID;
this.posts = [];
this.addPost = function(id, content) {
var post = new Post(id, content);
post.prototype = Category;
this.posts.push(post);
}
this.getPosts = function() {
for(post in this.posts){
this.posts[post].getContent();
}
}
}
/* Subclass */
function Post(postID, content) {
this.postID = postID;
this.content = content;
this.getContent = function() {
console.log('Post: '+ this.postID);
console.log('Category: ' + this.catID);
console.log('Content: ' + this.content);
}
}
var cat1 = new Category(1);
var cat2 = new Category(2);
cat1.addPost(101, 'First post in first category');
cat1.addPost(102, 'Second post in first category');
cat2.addPost(201, 'First post in second category');
cat2.addPost(202, 'Second post in second category');
cat1.getPosts();
cat2.getPosts();
我被困在了 post.prototype = Category
线上。我希望现在 Post
继承 Category
的属性,但它没有发生。
有人可以帮我解决这个问题吗?
I am quite experienced with coding in Javascript, but there's still one thing I can't really wrap my head around.
I have a superclass, let's say Category. Now I want to create some instances of a subclass, let's say Post, from inside the Category instance. I want Post to have its own properties, but it also needs to be able to access properties of its parent. So this is the concept:
/* Superclass */
function Category(catID) {
this.catID = catID;
this.posts = [];
this.addPost = function(id, content) {
var post = new Post(id, content);
post.prototype = Category;
this.posts.push(post);
}
this.getPosts = function() {
for(post in this.posts){
this.posts[post].getContent();
}
}
}
/* Subclass */
function Post(postID, content) {
this.postID = postID;
this.content = content;
this.getContent = function() {
console.log('Post: '+ this.postID);
console.log('Category: ' + this.catID);
console.log('Content: ' + this.content);
}
}
var cat1 = new Category(1);
var cat2 = new Category(2);
cat1.addPost(101, 'First post in first category');
cat1.addPost(102, 'Second post in first category');
cat2.addPost(201, 'First post in second category');
cat2.addPost(202, 'Second post in second category');
cat1.getPosts();
cat2.getPosts();
I got stuck on the line post.prototype = Category
. I would expect that now Post
inherits the properties of Category
, but it doesn't happen.
Can someone please help me out with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JavaScript 没有类。一个对象的原型是另一个对象。如果您将原型分配更改为此,它应该可以工作:
但是,我认为这不是您想要做的。在这种情况下,继承关系没有意义。
Post
并不是真正的Category
类型。 IMO,我会使用组合而不是继承:这样,从您的帖子中,您应该能够通过类别成员访问类别的成员。
JavaScript does not have classes. The prototype of an object is another object. If you change your prototype assignment to this, it should work:
I don't think this is what you want to do, however. An inheritance relationship does not make sense in this case. A
Post
is not really a type ofCategory
. IMO, I would use composition instead of inheritance:That way, from your post, you should be able to access the members of the category through the category member.