JavaScript
Validly embedding into a website:
<script>
/*<![CDATA[*/
alert('Correctly escaped js code, so it will still validate!');
/*]]>*/
</script>
!!
ist ein cast zum boolean
Object Oriented JavaScript
Simple object creation with new
and usage:
person = new Object() person.name = "Ano Nymous" person.height = "1.80" person.introduce = function() { alert('I am ' + this.name); } person.introduce();
Literal Notation:
person = { name: "Ano Nymous", height: "1.80", introduce: function() { alert('I am ' + this.name); } } person.introduce();
These 2 allow creation of objects. We still need classes though, instantiation and inheritance.
Function-class
function person(name) { this.name = name; this.height = "1.80"; this.introduce = function() { alert('I am ' + this.name); } } p1 = new person("Ano Nymous"); p1.introduce(); p2 = new person("Neva Eva"); p2.introduce();
Also works with var person = function(name) { …
instead of function person(name) { …
.
Prototyping – Exteding later on (req. for inheritance)
function person(name) {
this.name = name;
}
person.prototype.height = "1.80";
person.prototype.introduce = function() { alert('I am ' + this.name); }
p1 = new person("Ano Nymous");
p1.introduce();
Inheritance through Prototyping
Inheritance through functions
function sup() { this.up = function(){alert('SUPER');}; }
function sub() {
this.inheritFrom = sup;
this.inheritFrom();
this.down = function() {alert('GET DAOWN!');};
}
obj = new sub();
sub.down();
sub.up();