首页 > 编程语言 > 详细

javascript OOP 面向对象编程

时间:2015-04-17 17:24:58      阅读:202      评论:0      收藏:0      [点我收藏+]

Pseudo-class declaration

原文地址:http://javascript.info/tutorial/pseudo-classical-pattern#pseudo-class-declaration 

 

pseudo-class consists of the constructor function and methods.
For example, here’s the Animal pseudo-class with single method sit and two properties.

 

function Animal(name) {
  this.name = name
}

Animal.prototype = { 
  canWalk: true,
  sit: function() {
    this.canWalk = false
    alert(this.name + ‘ sits down.‘)
  }
}

var animal = new Animal(‘Pet‘) // (1)

alert(animal.canWalk) // true

animal.sit()             // (2)

alert(animal.canWalk) // false

  

  1. When new Animal(name) is called, the new object recieves __proto__ reference toAnimal.prototype, see that on the left part of the picture.
  2. Method animal.sit changes animal.canWalk in the instance, so now this animal object can’t walk. But other animals still can.

技术分享

 

The scheme for a pseudo-class:

  • Methods and default properties are in prototype.
  • Methods in prototype use this, which is the current object because the value of this only depend on the calling context, so animal.sit() would set this to animal.

javascript OOP 面向对象编程

原文:http://www.cnblogs.com/oxspirt/p/4435118.html

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