zoukankan      html  css  js  c++  java
  • dojo 官方翻译 dojo/_base/lang 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang

    应用加载声明:

    require(["dojo/_base/lang"], function(lang){
      // lang now contains the module features
    });

    clone()

     克隆任何对象或者元素节点,返回:一个新的对象。

    require(["dojo/_base/lang"], function(lang){
      // clone an object
      var obj = { a:"b", c:"d" };
      var thing = lang.clone(obj);
    
      // clone an array
      var newarray = lang.clone(["a", "b", "c"]);
    });

    delegate()

    返回值:委托的新对象。

    require(["dojo/_base/lang", function(lang){
      var anOldObject = { bar: "baz" };
      var myNewObject = lang.delegate(anOldObject, { thud: "xyzzy"});
      myNewObject.bar == "baz"; // delegated to anOldObject
      anOldObject.thud == undefined; // by definition
      myNewObject.thud == "xyzzy"; // mixed in from props
      anOldObject.bar = "thonk";
      myNewObject.bar == "thonk"; // still delegated to anOldObject's bar
    });

    exists()

    返回值:布尔类型。检查用点隔开的字符串的路径的所有对象是否存在,例如:“A.B.C”。第二个参数可选。

    require(["dojo/_base/lang", "dijit/dijit"], function(lang, dijit){
      var widgetType = "form.Button";
      var myNamespace = docs;
    
      if( lang.exists(widgetType, myNamespace) ){
        console.log("There's a docs.form.Button available");
      }else if( lang.exists(widgetType, dijit) ){
        console.log("Dijits form.Button class is available");
      }else{
        console.log("No form.Button classes are available");
      }
    });

    extend()
    返回值:返回扩展的类。功能和mixin类似。

    require(["dojo/_base/lang", "dijit/TitlePane"], function(lang, TitlePane){
      lang.extend(TitlePane, {
        randomAttribute:"value"
      });
    });

    getObject()

    返回值:以圆点分隔的字符串的对象的属性值。有第二个布尔类型的参数,可选,默认是false。第三个可选参数,是上下文,默认是global。

    require(["dojo/_base/lang"], require(lang){
      // define an object (intentionally global to demonstrate)
      foo = {
        bar: "some value"
      };
    
      lang.getObject("foo.bar"); // returns "some value"
    });

    第二个参数,给定是true,将会在属性不存在的情况下,创建该属性。

    require(["dojo/_base/lang"], function(lang){
      // define an object (intetionally global to demonstrate)
      foo = {
        bar: "some value"
      };
    
      // get the "foo.baz" property, create it if it doesn't exist
      lang.getObject("foo.baz", true); // returns foo.baz - an empty object {}
      /*
        foo == {
          bar: "some value",
          baz: {}
        }
      */
    });

    mixin()
    返回值:合并2个对象而成的新对象。合并从右向左,并重写左边对象。

    require(["dojo/_base/lang"], function(lang){
      var a = { b: "c", d: "e" };
      lang.mixin(a, { d: "f", g: "h" });
      console.log(a); // b: c, d: f, g: h
    });

    extend()和mixin() 的区别:
    extend,主要用户类的扩展;mixin,主要用户对象的扩展

    require(["dojo/_base/lang", "dojo/json"], function(lang, json){
      // define a class
      var myClass = function(){
        this.defaultProp = "default value";
      };
      myClass.prototype = {};
      console.log("the class (unmodified):", json.stringify(myClass.prototype));
    
      // extend the class
      lang.extend(myClass, {"extendedProp": "extendedValue"});
      console.log("the class (modified with lang.extend):", json.stringify(myClass.prototype));
    
      var t = new myClass();
      // add new properties to the instance of our class
      lang.mixin(t, {"myProp": "myValue"});
      console.log("the instance (modified with lang.mixin):", json.stringify(t));
    });

    replace()
    返回值:替换后的字符串。

    字典模式

    require(["dojo/_base/lang", "dojo/dom", "dojo/domReady!"], function(lang, dom){
      dom.byId("output").innerHTML = lang.replace(
        "Hello, {name.first} {name.last} AKA {nick}!",
        {
          name: {
            first:  "Robert",
            middle: "X",
            last:   "Cringely"
          },
          nick: "Bob"
        }
      );
    });

    数组模式:

    require(["dojo/_base/lang", "dojo/dom", "dojo/domReady!"], function(lang, dom){
      dom.byId("output").innerHTML = lang.replace(
        "Hello, {0} {2} AKA {3}!",
        ["Robert", "X", "Cringely", "Bob"]
      );
    });
    
    
    
    
    <p id="output"></p>

    方法模式:。最后还有自定义模式。

    require(["dojo/_base/array", "dojo/_base/lang", "dojo/dom", "dojo/domReady!"],
    function(array, lang, dom){
    
      // helper function
      function sum(a){
        var t = 0;
        array.forEach(a, function(x){ t += x; });
        return t;
      }
    
      dom.byId("output").innerHTML = lang.replace(
        "{count} payments averaging {avg} USD per payment.",
        lang.hitch(
          { payments: [11, 16, 12] },
          function(_, key){
            switch(key){
              case "count": return this.payments.length;
              case "min":   return Math.min.apply(Math, this.payments);
              case "max":   return Math.max.apply(Math, this.payments);
              case "sum":   return sum(this.payments);
              case "avg":   return sum(this.payments) / this.payments.length;
            }
          }
        )
      );
    });
    
    
    
    
    <p id="output"></p>

    setObject()
    给由点分隔的字符串的属性赋值。

    //之前的做法
    // ensure that intermediate objects are available
    if(!obj["parent"]){ obj.parent ={}; }
    if(!obj.parent["child"]){ obj.parent.child={}; }
    
    // now we can safely set the property
    obj.parent.child.prop = "some value";
    
    //现在
    require(["dojo/_base/lang"], function(lang){
      lang.setObject("parent.child.prop", "some value", obj);
    });

    trim()
    清除字符串2端的空格。

    require(["dojo/dom", "dojo/_base/lang", "dojo/domReady!"], function(dom, lang){
      function show(str){
        return "|" + lang.trim(str) + "|";
      }
      dom.byId("output1").innerHTML = show("   one");
      dom.byId("output2").innerHTML = show("two ");
      dom.byId("output3").innerHTML = show("   three ");
      dom.byId("output4").innerHTML = show("	four
    ");
      dom.byId("output5").innerHTML = show("f
    
    	vF I V Ef
    
    	v");
    });
    
    
    
    
    <p id="output1"></p>
    <p id="output2"></p>
    <p id="output3"></p>
    <p id="output4"></p>
    <p id="output5"></p>

     hitch()

     返回值:一个要执行的方法,附带了一个上下文(context)。

    require(["dojo/_base/lang"], function(lang){
      var myObj = {
        foo: "bar"
      };
    
      var func = lang.hitch(myObj, function(){
        console.log(this.foo);
      });
    
      func();
    });

    partial()
    和hitch()类似,返回值:方法。区别:没有提供context,传参。

    require(["dojo/_base/lang", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
    function(lang, dom, domConst, on){
      var myClick = function(presetValue, event){
        domConst.place("<p>" + presetValue + "</p>", "appendLocation");
        domConst.place("<br />", "appendLocation");
      };
    
      on(dom.byId("myButton"), "click", lang.partial(myClick, "This is preset text!"));
    });
    
    
    
    
    <button type="button" id="myButton">Click me to append in a preset value!</button>
    <div id="appendLocation"></div>
  • 相关阅读:
    struts2之JSP与Action的关系
    struts2之基本配置
    Android笔记之BroadCast判断网络状况
    Android笔记之监听左右滑动事件
    Android笔记之转到主线程运行
    springboot项目启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedde
    centos6 初次安装成功,未显示eth0网卡的信息
    layerui上传文件
    判断当前浏览器类型
    jquery重置下拉框
  • 原文地址:https://www.cnblogs.com/tiandi/p/3829804.html
Copyright © 2011-2022 走看看