标签:
一.创建对象
var car = new Object();
var car = {};
二、属性和方法
var car = {
color : "red",
run : function(){alert("run")}
};
两种形式可以搜索到属性:.和[]
car.color; // "red"
car.run(); // alert("run")
car["color"]; // "red"
car["run"](); // alert("run")
三 增加属性和方法
var car = {
color : "red",
run : function(){alert("run")}
};
car.type = "suv";
car.stop = function(){alert("stop")};
四 修改属性和方法
var car = {
color : "red",
run : function(){alert("run")}
};
car.color = "white";
car.run = function(){alert("run2")}; 重新赋值就可以了
五 删除属性和方法
var car = {
color : "red",
run : function(){alert("run")}
};
delete car.color;
car.color; // undefined
六、obj.constructor; 获取某个对象的构造函数
var car = {
color : "red",
run : function(){alert("run")}
};
car.constructor; // Object
var num = new Number(123);
num.constructor; // Number
七、obj.toString 对象转为字符串
var num = new Number(123);
num.toString(); // "123"
八、obj.valueOf 获取对象的原始值
var num = new Number(123);
num.valueOf(); // 123
九、obj.hasOwnProperty 检查自己有没有某个属性,结果返回布尔值
var car = {
color : "red",
run : function(){alert("run")}
};
car.hasOwnProperty("color"); // true
car.hasOwnProperty("logo"); // false
标签:
原文地址:http://www.cnblogs.com/zz334396884/p/4867445.html