Home > Web Front-end > JS Tutorial > body text

JavaScript object-oriented programming_javascript skills

WBOY
Release: 2016-05-16 19:06:00
Original
1258 people have browsed it

//Definition of class

//Method 1: General definition method of class
function player1(_name)
{
this.name = _name;
this.say = function () {alert(this.name);};
}

var p1 = new player1('llinzzi1');
p1.say();


//Method 2: prototype definition method
var player2 = function() {}
player2.prototype = {
name:'',
say:function(){
alert( this.name);
}
}

var p2 = new player2();
p2.name = 'llinzzi2';
p2.say();

                                                                                                   );
}
player3.prototype = {
init:function(_name){
this.name = _name;
say:function(){
alert(this.name);
}
}

var p3 = new player3('llinzzi3');
p3.say();

//Class Inheritance of

//Method 1
var player4 = function(){
this.init.apply(this, arguments);
}
player4.prototype = new player3;
player4.prototype.shout = function(){
alert(this.name.toUpperCase());
}

var p4 = new player4('llinzzi4');
p4.shout();


//Method 2 The above method cannot use the {} method, modify the method
Object.extend = function(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
};

var player5 = function(){
this.init. apply(this, arguments);
}
Object.extend(Object.extend(player5.prototype,player3.prototype),{
shout:function(){
alert(this.name. toUpperCase());
    }

  });

var p5 = new player5('llinzzi5');
p5.shout();




//From prototype.js Copy the browser judgment code

Browser = {
IE: !!(window.attachEvent && !window.opera),
Opera: !!window.opera,
WebKit: navigator .userAgent.indexOf('AppleWebKit/') > -1,
Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}

alert(Browser.MobileSafari);

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template