gpt4 book ai didi

javascript - Meteor - 模型继承

转载 作者:可可西里 更新时间:2023-11-01 10:04:12 33 4
gpt4 key购买 nike

在模型中创建继承的语法是什么(特别是使用 new Meteor.Collections 创建的 MongoDB 模型)?

我正在尝试对在 http://schema.org 上找到的一些模式进行建模,我真的很想使用继承来准确地建模它们。

最佳答案

不确定您是否仍在研究这个问题,但文档现在支持这种事情。来自文档:

// An Animal class that takes a document in its constructor
Animal = function (doc) {
_.extend(this, doc);
};

_.extend(Animal.prototype, {
makeNoise: function () {
console.log(this.sound);
}
});

// Define a Collection that uses Animal as its document
Animals = new Mongo.Collection("Animals", {
transform: function (doc) { return new Animal(doc); }
});

// Create an Animal and call its makeNoise method
Animals.insert({name: "raptor", sound: "roar"});
Animals.findOne({name: "raptor"}).makeNoise(); // prints "roar"

关于javascript - Meteor - 模型继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877193/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com