私は実際にこれを達成し、それはここに私のためにうまく働いたが「一瞬 インポートから
輸入瞬間、私のコード例である_「lodash」
class Model {
constructor(props) {
this.initialize(props)
}
initialize(props) {
this.id = props.id && Number(props.id) || null
this.createdAt = props.createdAt && moment(props.createdAt) || null
this.updatedAt = props.updatedAt && moment(props.updatedAt) || null
this.deletedAt = props.deletedAt && moment(props.deletedAt) || null
}
toJson() {
const props = Object.assign({}, this)
_.forOwn(props, (value, key) => {
if (value instanceof moment) {
props[key] = value.format('YYYY-MM-DD HH:mm:ss')
}
})
return props
}
}
import moment from 'moment'
import Model from './Model'
import User from './User'
class Article extends Model {
constructor(props) {
super(props)
this.initialize(props)
}
initialize(props) {
super.initialize(props)
this.slug = props.slug | ''
this.title = props.title || ''
this.description = props.description || ''
this.content = props.content || ''
this.published = props.published || false
this.publishedAt = props.publishedAt ? moment(props.publishedAt) : null
// relate user model
this.user = props.user ? new User(props.user) : null
}
}
export default Article
から私は簡単に記事を開始することができますかモデルクラスとインスタンスまたはスタティック関数を使用する
const Article = new Article({ // });
Article.toJson();
コンストラクタでSelectionオブジェクトを作成しないため、このように簡単にサブクラス化できません。代わりに、ブラウザはあなたのためのオブジェクトを作るファクトリ関数 'window.getSelection()'を提供します。だから、あなたは本当にそれがあなたが望むサブクラスオブジェクトを作成させる方法がありません。 – jfriend00
プロトタイプはどうですか? Selection.prototype.someNewMethod1 = function(){}は、あなたが望むものを正確に行います。 – bigless
これは、Selectionオブジェクトのプロトタイプに関数を追加しますが、 'window.getSelection()'を呼び出すときは使用できません。私は 'const mySel = window.getSelection()'のようなやり方で、 'mySel.someNewMethod1 = function(){}'というメソッドをタックするだけで終わりました。 'window.getSelection()'は毎回同じインスタンスを取得するので、 'Selection'が普通のコンストラクタであった場合と同じ方法でメソッドが存続します。 –