MooTools学习笔记——0201 Creating MooTools classes

 

1. we create a MooTools class using the following format:

var className = new Class({properties});

 

2. our Dog class

var Dog = new Class({
	Implements : [Options], 
	options : {
		name : 'Barkee', 
		type : 'Poodle',
		age : 4
	},
	initialize : function(options) {
		this.setOptions(options);
	},
	bark : function() {
		alert(this.options.name + ' is barking.');
	},
	sit : function() {
		alert(this.options.name + ' is sitting.');
	}
});

 

The Implements Property

The Implements Property basically tells our new class what other class properties/methods to include as part of our new class. Either its classes are created by us or our MooTools classes(like Options and Events).

Options and Events are MooTools utility classes in the Class.Extras components of MooTools. In our case, we are giving our class the instruction to implement the Options class.

Implements : [Options]

 

The Options class provides a way to deal with setting default options for our class, and automatically decides what options to overwrite and leave alone depending on what parameters are passed to it.

It also includes the .setOptioins method which is the method that triggers the setting of these options.

 

The options property

The options property lets us set default options for our Dog class.

 

The entire code:

<!DOCTYPE HTML>
<html>
<meta charset="GBK">
<head>
<script type="text/javascript" src="mootools-1.2.6-core-nc.js"></script>
<script type="text/javascript">
	var Dog = new Class({
		Implements : [Options], 
		options : {
			name : 'Barkee', 
			type : 'Poodle',
			age : 4
		},
		initialize : function(options) {
			this.setOptions(options);
		},
		bark : function() {
			alert(this.options.name + ' is barking.');
		},
		sit : function() {
			alert(this.options.name + ' is sitting.');
		}
	});
	var myDog = new Dog({});
	myDog.bark();
</script>
<title>Creating a MooTools class</title>
</head>
<body>
</body>
</html>

 

Preview the HTML document in your web browser :
MooTools学习笔记——0201 Creating MooTools classes
  

你可能感兴趣的:(MooTools学习笔记)