JavaScript:从超类实例内部创建子类

发布于 2024-12-17 10:09:07 字数 1276 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-12-24 10:09:07

JavaScript 没有类。一个对象的原型是另一个对象。如果您将原型分配更改为此,它应该可以工作:

post.prototype = this;

但是,我认为这不是您想要做的。在这种情况下,继承关系没有意义。 Post 并不是真正的 Category 类型。 IMO,我会使用组合而不是继承:

post.category = this;

这样,从您的帖子中,您应该能够通过类别成员访问类别的成员。

JavaScript does not have classes. The prototype of an object is another object. If you change your prototype assignment to this, it should work:

post.prototype = this;

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 of Category. IMO, I would use composition instead of inheritance:

post.category = this;

That way, from your post, you should be able to access the members of the category through the category member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文