首页 > 编程语言 > 详细

javascript 面向对象--理解

时间:2014-04-05 00:39:39      阅读:363      评论:0      收藏:0      [点我收藏+]

一般我们的原型写法都是

bubuko.com,布布扣
     var Father = function () {
           this.speed= 90;
        };
        Father.prototype.move = function (speed) {
            console.log("原型"+speed);
        };
        Father.prototype.start = function () {
            console.log(this.speed);
        };
        var father = new Father();
        father.start();//90
bubuko.com,布布扣

如果我们要在start里面调用move这个方法

直接用this指针就行了

bubuko.com,布布扣
var Father = function () {
           this.speed = 90;
        };
        Father.prototype.move = function (speed) {
            console.log("原型"+speed);
        };
        Father.prototype.start = function () {
            //console.log(this.speed);
            this.move(this.speed);
        };
        var father = new Father();
        father.start();//原型90
bubuko.com,布布扣

如果我们在start里面再写一个function,看下怎样

bubuko.com,布布扣
var Father = function () {
           this.speed = 90;
        };
        Father.prototype.move = function (index) {
            console.log("原型"+index);
        };
        Father.prototype.start = function () {
            //console.log(this.name);
            //this.move(this.speed);
            setInterval(function () {
                this.move(this.speed);
         },
2000); }; var father = new Father(); father.start();//error
bubuko.com,布布扣

出现错误,会提示不存在move属性或方法,为什么呢

 初步认为是setInterval里面的匿名函数,指针this没有指向外面的move,再修改一下

bubuko.com,布布扣
var Father = function () {
           this.speed = 90;
        };
        Father.prototype.move = function (index) {
            console.log("原型"+index);
        };
        Father.prototype.start = function () {
            //console.log(this.name);
            //this.move(this.speed);
            setInterval(function (self) {
                self.move(self.speed);
                self.speed++;
            }, 2000,this);
            //setInterval(function () { }, 2000);
        };
        var father = new Father();
        father.start();//在chrome下正常运行,但是在IE下就不行了
bubuko.com,布布扣

 

 我们把this指针传进去

bubuko.com,布布扣
var Father = function () {
        this.speed = 90;
        return Father.prototype;
    };
        Father.prototype.speed = 100;
        Father.prototype.move = function (speed) {
            console.log("原型" + speed);
        };
        Father.prototype.move1 = function (speed) {
            return function () { 
                this
            };
        }
        Father.prototype.start = function () {
            //console.log(this.name);
            //this.move(this.speed);
            //setInterval(function () {
            //self.move(self.speed);
            //this.move(this.speed);
            //self.speed++;
            //return this;
            //}, 2000);
            //setInterval(function () { }, 2000);
            //(function adds(self) {
                //console.log("ddd");
                //self.move("ddddddd");
            //})(this);

            //function adddd(self) {
               // self.move("hh");
            //};
            //adddd(this);

            var self = this;
            setInterval(function () {
                self.move("ddddddddddddddddddddddddddd");
            }, 2000);
        };
        var father = new Father();
        father.start();//原型
bubuko.com,布布扣

 

搞掂

 

javascript 面向对象--理解,布布扣,bubuko.com

javascript 面向对象--理解

原文:http://www.cnblogs.com/lihaozhou/p/3645255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!