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("\tfour\r\n");
  dom.byId("output5").innerHTML = show("\f\n\r\t\vF I V E\f\n\r\t\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>

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM