Officially translated dojo/_ base/lang version 1.10

Source: Internet
Author: User

Official Address: http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang

Application load declaration:

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

Clone ()

Clone any object or element node and return a new object.

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 ()

Returned value: the new object of the 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 ()

Return Value: boolean type. Check whether all objects in the path of the string separated by dots exist, for example, "A. B .C ". The second parameter is optional.

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 ()
Return Value: return the extended class. Similar to Mixin.

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

GetObject ()

Returned value: the property value of the object of a string separated by dots. There is a second Boolean parameter. (optional) The default value is false. The third optional parameter is context. The default parameter is 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"});

If the second parameter is set to true, the property is created if the property does not exist.

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 ()
Return Value: a new object that combines two objects. Merge objects from right to left and rewrite the objects on the left.

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});

Differences between extend () and Mixin:
Extend: extension of main user classes; Mixin: extension of main user objects

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 ()
Return Value: the string after replacement.

Dictionary Mode

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"    }  );});

Array mode:

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>

Method mode :. The custom mode is also available.

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 ()
Assign values to the properties of a string separated by vertices.

// Previous practice // ensure that intermediate objects are availableif (! OBJ ["parent"]) {obj. Parent = {};} if (! OBJ. parent ["child"]) {obj. parent. child ={};}// now we can safely set the propertyobj. parent. child. prop = "some value ";

 

// Now require (["dojo/_ base/lang"], function (Lang) {Lang. setobject ("parent. child. prop "," some value ", OBJ );});

Trim ()
Removes spaces at the end of string 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 ()

Returned value: A method to be executed with a context ).

 

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

Partial ()
Similar to hitch (), return value: method. Difference: context is not provided for passing parameters.

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>

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.