官术网_书友最值得收藏!

Time for action—creating an instance of Dog

Let's create an instance of a Dog class by deploying the following the steps:

  1. Create an HTML document for this. Use the following code taken from our Dog class.
    <html>
    <head>
    <script type="text/javascript" src="mootools-1.2.1-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.' );
    }
    });
    </script>
    </head>
    <body>
    </body>
    </html>
    

    Tip

    Apart from taking out the comments, referencing the MooTools framework, and putting in the basic tags for an HTML document, nothing has changed so far.

  2. Create an instance of our Dog class, you can call the class instance anything you like, but I'll call mine simply myDog. Place the following code right below our class declaration.
    var myDog = new Dog({ });
    

    You've now created an instance of Dog, but we didn't declare any values for options properties, so the options for myDog will be set to our default values that we declared in the class.

  3. Let's make myDog bark. Right below the code that we just wrote, place the following code, which will pass the .bark() method to myDog:
    myDog.bark();
    
  4. Save your work and preview the HTML document in the web browser. It should immediately open up a dialog box like this:
    Time for action—creating an instance of Dog

What just happened?

We just created a new instance of the Dog class called myDog. We didn't give myDog any replacement options, so it used as the default option in our class. When we used the .bark() method on myDog, it alerted us that "Barkee", the default name, "is barking".

If you followed along, this is the entire code you should have thus far:

<html>
<head>
<script type="text/javascript" src="mootools-1.2.1-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>
</head>
<body>
</body>
</html>

Have a go hero — — use the .sit() class method

We still haven't used the .sit() method. Why don't you modify our example to use the .sit() method on myDog instead of .bark()?

主站蜘蛛池模板: 丰都县| 格尔木市| 平乡县| 喀喇沁旗| 兴宁市| 浮山县| 汝城县| 志丹县| 镇赉县| 罗平县| 顺昌县| 海盐县| 乌鲁木齐县| 英德市| 墨竹工卡县| 鄂伦春自治旗| 全椒县| 湟中县| 会昌县| 拜泉县| 建瓯市| 赣榆县| 巢湖市| 乌海市| 怀安县| 浪卡子县| 大化| 大厂| 鹰潭市| 和龙市| 庆安县| 正定县| 贵港市| 高阳县| 揭东县| 延寿县| 齐齐哈尔市| 龙江县| 芦山县| 漠河县| 平度市|