初次接触mootools

以下是今天所学代码,网上有这篇博客可供参考,另外还是推荐官方文档 ,以下是今天所敲代码:

//用mootools创建类的方式:

//方式1:用标准方式传入一个对象字面量

/*	var Person = new Class({
		initialize:function(name,age){
			this.name=name;
			this.age=age;
		},

		log:function(){
			console.log(this.name+","+this.age);
		}
	});

	var mark = new Person("Tom",20);
	mark.log();
*/


//方式2:传入一个普通函数然后用implement方法对类进行扩展:

/*	var Person = new Class(function(name,age){
		this.name=name;
		this.age=age;
	});

	//用implement惊醒扩展:
	Person.implement("log",function(){
		console.log(this.name+","+this.age);
	});

	var mark=new Person("cat",22);
	mark.log();
*/
	
	//想扩展多个就多次使用implement
/*
	var Person = new Class(function(name,age){
		this.name=name;
		this.age=age;
	});

	//用implement多次扩展:
	Person.implement("getname",function(){
		console.log(this.name);
	});

	Person.implement("getage",function(){
		console.log(this.age);
	});

	var mark=new Person("cat",22);
	mark.getname();
	mark.getage();

*/







//上述用implement进行扩展每次只能扩展一个,想扩展多个可用字面量的方式

/*
	var Person = new Class(function(name,age){
		this.name=name;
		this.age=age;
	});

	//用字面量的方式用implement扩展:
	Person.implement({
		"getname":function(){   //注意:getname 加不加分号都行
			console.log(this.name);
		},
		"getage":function(){
			console.log(this.age);
		}
	});

	var mark=new Person("cat",22);
	mark.getname();
	mark.getage();
	
*/


/*
	mootools中为对象添加属和方法用implement,
	为类添加属性和方法用extend
	注意:
		实例不能调用为类创建的方法
		类不能直接调用为实例创建的方法
		类可以通过  类名.prototype.实例上的方法

*/





/*
	var Person = new Class(function(name,age){
		this.name=name;
		this.age=age;
	});
	Person.implement({
		instancemethod:function(){
			console.log("这是实例方法");
		}
		
	});
	Person.extend({
		classmethod:function(){
			console.log("这是类方法");
		}
	});

	var mark = new Person("jack",33);
	mark.instancemethod();
	//mark.classmethod();  //出错,实例不能调用类上的方法
	Person.classmethod();  
	//Person.instancemethod();//出错,类不能嗲用实例上的方法
	//可通过prototype的方式调用:
	Person.prototype.instancemethod();
*/



//私有成员
// 用闭包为类创建私有成员

	/*var Person = (function(){
		//私有变量
		var privatevalue = 0;
		//私有方法
		var privatemethod = function(name){
			return name;
		}

		return new Class({
			initialize:function(name,age){
				this.name = name;
				this.age = age;

				privatevalue++;//使用外部的privatevalue形成闭包

			},
			//共有方法
			log:function(){
				console.log(privatemethod(this.name)+","+this.age);
			},
			getprivatevalue:function(){
				return privatevalue;
			}
		});

	})();

	var mark = new Person("mark",24);
	mark.log();
	console.log(mark.getprivatevalue());
	//console.log(mark.getprivatevalue());

*/



//继承

/*
//创建一个基类:
 	var Animal = new Class({
 		initialize:function(age){
 			this.age=age;
 		}
 	});
//创建一个子类:
	var Cat = new Class({
		Extends:Animal,
		initialize:function(name,age){
			this.parent(age);//使用基类中的
			this.name=name;
		}
	});

	var cat = new Cat("rose",20);
	console.log(cat.name);
	console.log(cat.age);

*/

//用Extends实现了(单)继承
/*
	var Fa = new Class({
		add:function(a,b){
			return a+b;
		},
		sub:function(a,b){
			return a-b;
		}
	});

	var Son = new Class({
		Extends:Fa,
		mul:function(a,b){
			return a*b;
		}
	});

	var son1 = new Son();
	console.log(son1.add(1,2));
	console.log(son1.sub(1,2));
	console.log(son1.mul(1,2));

*/



	
	/*var Fa = new Class({
		add:function(a,b){
			return a+b;
		}
	});

	var Fa2 = new Class({
		add:function(a,b){
			return a-b;
		}
	});

	var Son = new Class({
		Implements:Fa2,
		Extends:Fa  //两者没有严格的优先级,后面的总能重写前面的
	
	});

	var son1 = new Son();
	console.log(son1.add(1,2));*/


原文地址:https://www.cnblogs.com/chayangge/p/4288670.html