zoukankan      html  css  js  c++  java
  • win8: WinJS的面向对象 WinJS.Class

    官方文档:http://msdn.microsoft.com/en-us/library/windows/apps/br229776.aspx

    WinJS.Class.define 类定义

    var object = WinJS.Class.define(constructor, instanceMembers, staticMembers);

    三个参数是:构造函数,实例成员集合,静态成员集合。e.g.

    var Robot = WinJS.Class.define( function(name) {
        this.name = name;
    }, 
    { modelName: "" }, 
    { harmsHumans: false });
    
    var myRobot = new Robot("Mickey");
    
    myRobot.modelName = "4500";
    Robot.harmsHumans = false;

    对应的js代码是:

    function Robot(name) {
        this.name = name;
    }
    
    Robot.prototype.modelName = '4500';
    Robot.harmsHumans = false;

    在JS中用 Object.defineProperty 定义modelName GET/SET时我们需要这么做:

    Object.defineProperty(Robot.prototype, "modelName", {
        get: function () { this.computeModelName(); }
    });

    但在WinJS中:

    var Robot = WinJS.Class.define(
        function (name) { this.name = name; },
        {
            modelName: { get: function () { return this.computeModelName(); } }
        }
    );

     

    WinJS.Class.derive 继承

    var object = WinJS.Class.derive(baseClass, constructor, instanceMembers, staticMembers);

    它通过调用Object.create 使用baseClass 的prototype 构造派生对象。

    var SpaceRobot = WinJS.Class.derive(Robot, function (name) {
        this.name = name;
    },
    { airSupply: "" },
    { saveSelf: true });
    
    var mySpaceRobot = new SpaceRobot("Myra");
    mySpaceRobot.airSupply = "oxygen";
    var save = SpaceRobot.saveSelf;

    WinJS.Class.mix 混合类

    var object = WinJS.Class.mix(constructor);
    WinJS.Namespace.define("Robotics", {
        Robot: WinJS.Class.define( function(name) {
            this.name = name;
            }, 
            { name: name }, 
            { harmsHumans: false, obeysOrders: true })
        }); 
    
    var Movable = {
        goForward: function () {
            document.getElementById("div").innerText = "go forward";
        },
        goBack: function () {
            document.getElementById("div").innerText = "go back";
        },
        turnRight: function () {
            document.getElementById("div").innerText = "turn right";
        },
        turnLeft: function () {
            document.getElementById("div").innerText = "turn left";
        };
    
    WinJS.Class.mix(Robotics.Robot, Movable);
    
    var myRobot = new Robotics.Robot("Mickey");
    
    myRobot.goBack();

    当对象与WinJS.Utilities.eventMixin 等混合时,可以获取事件监听的功能等......

    More info about mix , Check:http://msdn.microsoft.com/en-us/library/windows/apps/hh967789.aspx

     

     

     

     

     

     

  • 相关阅读:
    杭电2074
    关于大整数n!的问题!
    杭电2053
    大整数乘法(高精度)
    JS:获取框架内容
    JQ:hover延迟效果
    jQ+Ajax+PHP 简单实例
    js节省document.getElementById("xxx")的方法
    QQ一键登录功能的实现过程
    点击文字出现文本框
  • 原文地址:https://www.cnblogs.com/mybkn/p/2711519.html
Copyright © 2011-2022 走看看