js学习 伪类及原型

构造伪类来继承Mammal,通过定义它的constructor函数并替换它的prototype为一个mammal的实例来实现
var Mammal = function(name) {
	this.name = name;
};
Mammal.prototype.get_name = function() {
	return this.name;
};
Mammal.prototype.says = function() {
	return this.saying || '';
};

var Girl = function(name) {
	this.name = name;
	this.saying = 'you';
	//this.girl = 'fang';
};

Girl.prototype = new Mammal();

Girl.prototype.girl = function() {
	return 'fang';

};

Girl.prototype.purr = function(n) {
	var i, s = '';
	for (i = 0; i < n; i++) {
		if (s) {
			s += '-';
		}
		s += 'r';
	}
	return s;
};
Girl.prototype.get_name = function() {
	return this.says() + ' ' + this.name + ' ' + this.girl();
};

var myGirl= new Girl('love');
var says = myGirl.says();
var purr = myGirl.purr(5);
var name = myGirl.get_name();

document.writeln('  ' + says + '  ' + purr + '  ' + name + '  ');


原型  差异化继承
if (typeof Object.beget !== 'function') {
	Object.beget = function(o) {
		var F = function() {
		};
		F.prototype = o;
		return new F();
	}
};
var myPeople = {
	name : 'qiang love fang',
	get_name : function() {
		return this.name;
	},
	says : function() {
		return this.saying || '';
     }

};

var myGirl=Object.beget(myPeople);
myGirl.name='fang';
myGirl.saying='love';
myGirl.purr=function(n){
	var i,s='';
	for(i=0;i<n;i++){
		if(s){s+='-'}
		s+='r';
}
	return s;
};
myGirl.get_name=function(){
	return this.says()+' '+this.name+' '+this.says();
};
var name=myGirl.get_name();
document.writeln(name);

你可能感兴趣的:(JavaScript,F#,prototype)