对象是什么?
person=new Object();//定义一个对象 person.name="jiaxin";//对象的属性 person.age=21;
person.sex="female";
或
person={name:"jiaxin",age:21,sex:"female"};
或者使用构造器
function person(name,age,sex)
{ this.name=name; this.age=age; this.sex=sex; }
var me=new people("jiaxin","21","female")
判断对象是否为空
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("li").click(function(){
person=new Object();
people=new Object();
people.name="jiaxin";
if (isEmptyObject(person)) {
console.log(1);
}
if (isEmptyObject(people)) {
console.log(2);
}
function isEmptyObject(obj) {
for (var key in obj) {
return false;
}
return true;
}
});
});
</script>
可知person是空的对象,people是不为空的对象,所以结果是输出1。