[Edge]廃止→
廃止: この機能は廃止されました。まだいくつかのブラウザーで動作するかもしれませんが、いつ削除されてもおかしくないので、使わないようにしましょう。
このメソッドは、オブジェクトのソースコードを表す文字列を返します。
| obj | Object。 |
|---|
オブジェクトのソースコードを表す文字列です。
toSource() メソッドは以下の値を返します。
function Object() {
[native code]
}
デバッグ時に toSource() を呼び出して、オブジェクトの内容を調べることができます。
オブジェクトが toSource() メソッドをオーバーライドしても安全です。例えば次のコードを見てください。
function Person(name) {
this.name = name;
}
Person.prototype.toSource = function Person_toSource() {
return 'new Person(' + uneval(this.name) + ')';
};
console.log(new Person('Joe').toSource()); // ---> new Person("Joe")
¶省く
次のコードは Dog オブジェクト型を定義して theDog、すなわち Dog 型のオブジェクトを生成します。
function Dog(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}
theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
D=theDog.toSource(); //={name:"Gabby", breed:"Lab", color:"chocolate", sex:"female"}