Pseudo-class Declaration
Original address: Http://javascript.info/tutorial/pseudo-classical-pattern#pseudo-class-declaration
A Pseudo-class consists of the constructor function and methods.
For example, here's the Pseudo-class with single method and both Animal
sit
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)//Trueanimal.sit () //(2) alert (Animal.canwalk)//False
- When
new Animal(name)
was called, the new object recieves __proto__
reference to, see this on Animal.prototype
the "the" picture.
- Method
animal.sit
Changes animal.canWalk
in the instance, so now this animal object can ' t walk. But the other animals still can.
The scheme for a pseudo-class:
- Methods and Default properties is in prototype.
- Methods
prototype
in use this
, which are the current object because the value of only depend on the this
calling Co ntext, so animal.sit()
would set this
to animal
.
JavaScript OOP object-oriented programming