对象、原型

2016-09-23  本文已影响53人  StarLikeRain

问答


<script>
    function zxc(name, age) {
        this.a = name;
        this.b = age;
    }
    zxc.prototype = {
        behavior: function () {
            console.log(this.a + ' is ' + this.b + ' yep');
        }
    }
    var asd = new zxc('I-wipe', 3);
    asd.behavior();//console.log('I-wipe is 3 yep');
</script>

prototype...
function People (name){
  this.name = name;
  this.sayName = function(){
    console.log('my name is:' + this.name);
  }
}

People.prototype.walk = function(){
  console.log(this.name + ' is walking');  
}

var p1 = new People('谷谷谷鸟');
var p2 = new People('前端');

原型图待做......


function People (){
  var age = 1//局部变量age = 1;
  this.age = 10;//谁调用谁会获得一个buff。age = 10;
}
People.age = 20;//给函数对象一个age = 20。看图

People.prototype.age = 30;//给函数的原型一个age = 30;
age.png

代码

<script>
    function Car(name, color, status) {
        this.name = name;
        this.color = color;
        this.status = status;
    }
    Car.prototype = {
        run : function () {
            console.log('runnnnn')
        },
        stop : function () {
            console.log('stoppppppp')
        },
        getStatus: function () {
            console.log(this.status)
        }
    }
    var car = new Car('a', 'b', 'c');
    car.run();
    car.stop();
    car.getStatus();
</script>


function Carousel($node){
//todo...
}
Carousel.prototype = {
//todo ..
};
var $node1 = $('.ct').eq(0);
var $node2 = $('.ct').eq(1);
var carousel1 = new Carousel($node1);
var carousel2 = new Carousel($node2);

Object-tag-pages

上一篇 下一篇

猜你喜欢

热点阅读