JS中对象的定义及相关操作

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-1.7.1.js"></script>
</head>
<body></body>
</html>
<script type="text/javascript">
    var obj = {};
    obj = {
        name: '张三',
        friends: [{ name: '小李' }, { name: '小红' }],
        eat: function () { alert('I am eating.'); }
    }
     
    // 调用对象中方法
    obj.eat();

    // 遍历对象集合
    $.each(obj.friends, function (index, item) {
        alert(index);
        alert(item.name);
    });
</script>

 

posted @ 2015-01-19 19:24  astrue  阅读(256)  评论(0编辑  收藏  举报