Fork me on GitHub

js对象使用

以下是js对象使用的两种方式

<script type="text/javascript">
        var people = function () {
         
        }
        
        //方法1
        people.prototype =
        {
         age: 18,
         name:'jack',
         sayHello:function(){
                alert("你好");
            }
        }

        //方法2
        function MyObject() {
           var name = "myObject";
            this.type = "class";
            this.methodA = function () {
                alert(this.name);
            }
            this.methodB = function () {
                return this.type;
            }
        }



        window.onload = function () {
            var p = new people();
            p.age = 20;
            alert(p.age);

            var myObject = new MyObject();
            myObject.name = 'hahhahah'
            alert(myObject.name);
            myObject.methodA();
            var type = myObject.methodB();
            alert(type);
        }
    </script>

 

  

posted @ 2014-04-09 13:41  Aigu  阅读(284)  评论(0编辑  收藏  举报