これが以前に回答されている場合は申し訳ありませんが、Javascript でカスタム オブジェクトを作成することに関して提供される選択肢の量に少し圧倒されます。それぞれの長所や短所、またはそれらがまったく異なるかどうかはわかりません。
オブジェクトを構築するために私が見つけたさまざまな方法のいくつかを次に示します。
1: 新しいオブジェクト
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
2: リテラル表記
timObject = {
property1 : "Hello",
property2 : "MmmMMm",
property3 : ["mmm", 2, 3, 6, "kkk"],
method1 : function(){alert("Method had been called" + this.property1)}
};
3: 機能
function AdBox() {
this.width = 200;
this.height = 60;
this.text = 'default ad text';
this.prototype.move = function() {
// code for move method goes here
}
}
this.prototype.display = function() {
// code
}
私はいくつかの方法を見ましたが、それらはあまり一般的ではないようでした..ご覧のとおり、フィールドとメソッドを持つ単純なオブジェクトが必要な場合の標準が何であるかは正確にはわかりません.
読んでくれてありがとう。