function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}
d = new Date();
document.getElementById("demo").innerHTML = d.toJSON();
Property Getters and Setters
ES5では、プロパティを取得または設定するような構文でオブジェクトメソッドを定義できます。
この例では、fullName というプロパティのgetterを作成します:
例
// Create an object:
var person = {
firstName: "John",
lastName : "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
この例では、languageプロパティの*setter*と*getter*を作成しています:
例
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
get lang() {
return this.language;
},
set lang(value) {
this.language = value;
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
この例では、setterを使用して言語の大文字の更新を保護します。
例
var person = {
firstName: "John",
lastName : "Doe",
language : "NO",
set lang(value) {
this.language = value.toUpperCase();
}
};
// Set an object property using a setter:
person.lang = "en";
// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
// Create an Object:var person = { firstName: "John", lastName : "Doe", language : "NO", }; // Change a Property:Object.defineProperty(person, "language", { value: "EN", writable : true, enumerable : true, configurable : true}); // Enumerate Propertiesvar txt = "";for (var x in person) { txt += person[x] + "
";}document.getElementById("demo").innerHTML = txt;
次の例は、言語プロパティを列挙から隠していることを除いて、同じコードです。
例
// Create an Object:var person = { firstName: "John", lastName : "Doe", language : "NO", }; // Change a Property:Object.defineProperty(person, "language", { value: "EN", writable : true, enumerable : false, configurable : true}); // Enumerate Propertiesvar txt = "";for (var x in person) { txt += person[x] + "
";}document.getElementById("demo").innerHTML = txt;
この例では、setterとgetterを作成して、言語の大文字の更新を保護します。
例
/// Create an Object:var person = { firstName: "John", lastName : "Doe", language : "NO"}; // Change a Property: Object.defineProperty(person, "language", { get : function() { return language }, set : function(value) { language = value.toUpperCase()}}); // Change Languageperson.language = "en"; // Display Language document.getElementById("demo").innerHTML = person.language;
E5 ObjectMethods
ES5では、JavaScriptに多くの新しいオブジェクトメソッドが追加されました。
オブジェクトの管理
// Create object with an existing object as prototype
Object.create(parent, donor)
// Adding or changing an object property
Object.defineProperty(object, property, descriptor)
// Adding or changing object properties
Object.defineProperties(object, descriptors)
// Accessing Properties
Object.getOwnPropertyDescriptor(object, property)
// Returns all properties as an array
Object.getOwnPropertyNames(object)
// Accessing the prototype
Object.getPrototypeOf(object)
// Returns enumerable properties as an array
Object.keys(object)
オブジェクトの保護
// Prevents adding properties to an object
Object.preventExtensions(object)
// Returns true if properties can be added to an object
Object.isExtensible(object)
// Prevents changes of object properties (not values)
Object.seal(object)
// Returns true if object is sealed
Object.isSealed(object)
// Prevents any changes to an object
Object.freeze(object)
// Returns true if object is frozen
Object.isFrozen(object)
person = {
firstName: "John",
lastName: "Doe",
age: 46,
}
配列の例
points = [
1,
5,
10,
25,
40,
100,
];
警告 !!!
JSONは、末尾のコンマを使用できません。
JSONオブジェクト:
// Allowed:
var person = '{"firstName":"John", "lastName":"Doe", "age":46}'
JSON.parse(person)
// Not allowed:
var person = '{"firstName":"John", "lastName":"Doe", "age":46,}'
JSON.parse(person)