基础 - 面向对象

面向对象

  程序员必须具备的开发思想之一

对象

  复杂的数据类型之一

  带有 属性 和 方法

对象的声明方式

var oWeWeZhang = new Object();

 

  var oWeWeZhang = {};
    oWeWeZhang.name = "WeWeZhang";
    oWeWeZhang.age = 18;
    oWeWeZhang.funFire = function() {
        console.log(this);
    }
    oWeWeZhang.funFire();

  

对象的封装函数

function Student(sName,nAge,funFire) {
        var obj = {};
        obj.name = sName;
        obj.age = nAge;
        obj.funFire = funFire;
        return obj;
    }
    var oWeWeZhang = Student("WeWeZhang",18, function () {
        console.log(this);
    });
    oWeWeZhang.funFire();

  

 new的作用 是 使this指向新的对象 

因此 构造函数 的写法为

    function Student(sName,nAge) {
        this.name = sName;
        this.age = nAge;
    }
    Student.prototype.funRead = function () {
        console.log(this);
    }
    var oWeWeZhang = new Student("WeWeZhang",18);
    oWeWeZhang.funRead();

  

  

  

posted @ 2016-08-19 14:17  WeWeZhang  阅读(132)  评论(0编辑  收藏  举报