JavaScript syntax: Difference between revisions

Content deleted Content added
Quamaretto (talk | contribs)
Creating inheritance manually does not belong here, esp. since this is a 'syntax' article.
Line 473:
Base::BaseFunction()
Base::Override() // mozilla only
 
Object hierarchy may also be created without prototyping:
 
function red() {
this.sayRed = function () {
alert ('red wine')
}
}
function blue() {
this.sayBlue = function () {
alert('blue sky')
}
this.someName = black // inherits black
this.someName() // inherits black
}
function black () {
this.sayBlack = function () {
alert('black night')
}
}
function anyColour() {
this.anotherName = red // inherits red
this.anotherName() // inherits red
this.sayPink = function() {
alert('"Any Colour You Like" is a song of Pink Floyd')
}
this.anotherName = blue // inherits blue ( + black )
this.anotherName() // inherits blue ( + black )
this.anotherName = 'released 1973' // now it's a string - just for fun
}
var hugo = new anyColour()
hugo.sayRed()
hugo.sayBlue()
hugo.sayBlack()
hugo.sayPink()
alert(hugo.anotherName)
 
Another way to perform inheritance is the use of Function methods ''apply'' and ''call'':
function AB ( a , b ) { this.a = a; this.b = b }
function CD ( c , d ) { this.c = c; this.d = d }
function ABCD ( a , b , c , d ) {
AB.apply ( this , arguments ) // inherits constructor AB
CD.call ( this , c , d ) // inherits constructor CD
this.get = function () { return this.a + this.b + this.c + this.d }
}
var test = new ABCD ( 'another ' , 'example ' , 'of ' , 'inheritance' )
alert ( test.get () )
 
==Exceptions==