Novice Learning Cordova-android JS Source (2)

Source: Internet
Author: User

Just a few, Cordova.js defined 17 modules, the following slowly look at the 17 modules.

First look at the more independent modules, some tool modules.

1. Cordova/urlutil

The object of the module output contains a method Makeabsolute, which translates the address into an absolute address.

Mark
function (Require, exports, module) { function makeabsolute (URL) { var anchorel = Document.createelement (' a '); = URL; return Anchorel.href;}; });

2. Cordova/utils

This module has more methods than

//File:src/common/utils.jsDefine ("Cordova/utils",function(Require, exports, module) {varUtils =exports; //Add the Get/set method for the key property of the Obj object    //Object, defineproperty/__definegetter__/__definesetter__Utils.definegettersetter =function(obj, key, Getfunc, Opt_setfunc) {if(object.defineproperty) {vardesc ={get:getfunc, configurable:true            }; if(opt_setfunc) {Desc.set=Opt_setfunc;        } object.defineproperty (obj, key, desc); } Else{obj.__definegetter__ (key, Getfunc); if(Opt_setfunc) {obj.__definesetter__ (key, Opt_setfunc);    }        }    }; Utils.definegetter=Utils.definegettersetter; //Array, IndexOfUtils.arrayindexof =function(A, item) {if(a.indexof) {returnA.indexof (item); }        varLen =a.length;  for(vari = 0; i < Len; ++i) {if(A[i] = = =Item) {                returni; }        }        return-1;    }; //Array, SpliceUtils.arrayremove =function(A, item) {varindex =Utils.arrayindexof (A, item); if(Index!==-1) {a.splice (index,1); }        returnIndex!==-1;    }; //Output Object Type "[Object Type]"Utils.typename =function(val) {returnObject.prototype.toString.call (val). Slice (8,-1);    }; Utils.isarray=function(a) {returnUtils.typename (a) = = = ' Array ';    }; Utils.isdate=function(d) {returnUtils.typename (d) = = = ' Date ';    }; //Copy object, deep clone objectUtils.clone =function(obj) {//note the place, typeof [date| array| Regular] is Object        if(!obj | |typeofobj = = = ' function ' | | Utils.isdate (obj) | |typeofObj!== ' object ') {            returnobj; }        //Recursive invocation        varRetVal, I; //Array Copy        if(Utils.isarray (obj)) {RetVal= [];  for(i = 0; i < obj.length; + +)i) {Retval.push (Utils.clone (obj[i)); }            returnRetVal; }        //Object CopyRetVal = {};  for(Iinchobj) {            if(! (IinchRetVal) | | Retval[i]! =Obj[i]) {Retval[i]=Utils.clone (Obj[i]); }        }        returnRetVal;    }; //Returns A wrapped version of the function, Aplly/call, sets the context for the execution of the modeUtils.close =function(Context, func, params) {if(typeofparams = = = ' undefined ') {            return function() {                returnfunc.apply (context, arguments);        }; } Else {            return function() {                returnfunc.apply (context, params);        };    }    }; //prototype, constructor, these two are very important for understanding JS inheritance.Utils.extend = (function() {        //Proxy used to establish prototype chain        varF =function() {}; //extend child from Parent        return function(Child, Parent) {F.prototype=Parent.prototype; Child.prototype=NewF (); child.__super__=Parent.prototype; Child.prototype.constructor=Child ;    };    } ()); Utils.alert=function(msg) {/*if (window.alert) {window.alert (msg); } else if (console && console.log) {*/Console.log (msg); // }    }; Utils.createuuid=function() {        returnUuidcreatepart (4) + '-' + uuidcreatepart (2) + '-' + uuidcreatepart (2) + '-' + uuidcreatepart (2) + '-' + uuidcreatepart (6);    }; //returns the corresponding 16 binary digits    functionUuidcreatepart (length) {varUuidpart = "";  for(vari = 0; i < length; i++) {            varUuidchar = parseint ((Math.random () * ()). ToString (16); //mark if(Uuidchar.length = = 1) {Uuidchar= "0" +Uuidchar; } Uuidpart+=Uuidchar; }        returnUuidpart; }});

1). definegettersetter, parameter obj, key, Getfunc, Opt_setfunc, defines the access method for the key property of obj.

There are several ways to pay attention to the implementation of Object.defineproperty and __definegetter__ (__definesetter__), which is the non-standard method behind it. Developer.mozilla.org is very detailed, the following is the link

Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineSetter__

2). Arrayindexof, parameter A, item, find index, this is very simple.

3). Arrayremove, parameter A, item, deletes an index in the array.

4). TypeName, IsArray, isDate, determine the variable type.

5). Clone, variable copy, note the difference between a value type and a reference type is OK.

6). Close, parameter context, Func, params: see this method to realize that the use of the application method can be compared with the call method to learn,

http://blog.csdn.net/myhahaxiao/article/details/6952321

7). Extend, inherited method. Prototype and constructor, prototypes and constructors, with these two to do JS inside the inheritance, find good articles on the Internet, recommend everyone read,

http://blog.csdn.net/niuyongjie/article/details/4810835

8). Alert

9). Createuuid, universal unique identification code. The return is 16-binary, math.random () randomly generated.

3. Cordova/base64

Conversion of Base64 and Arraybuffer

Define ("Cordova/base64",function(Require, exports, module) {varBase64 =exports; //refer to Arraybuffer related knowledge, http://javascript.ruanyifeng.com/stdlib/arraybuffer.html    //Uint8array = An array of 8-bit unsigned integers, 1 bytes in length. This is Arraybuffer view.Base64.fromarraybuffer =function(arrayBuffer) {varArray =NewUint8array (ArrayBuffer); returnuint8tobase64 (array);    }; //Atob = Base64 Decoding method    //Btoa = Base64 encoding MethodBase64.toarraybuffer =function(str) {//To decode the str .        //using the Window.atob method        //with buffer = buffer in node. js, the server-side JS        varDecodedstr =typeofAtob!== ' undefined '? Atob (str):NewBuffer (str, ' base64 '). toString (' binary '); //allocates memory of the corresponding size byte, typed array        varArrayBuffer =NewArrayBuffer (decodedstr.length); //Create an operation view of Arraybuffer        varArray =NewUint8array (ArrayBuffer);  for(vari = 0, len = decodedstr.length; i < Len; i++) {            //Uint8array, it's 8-bit.            //charCodeAt, returns 0-65535, 16 bits, 2 bytesArray[i] =decodedstr.charcodeat (i); }        returnArrayBuffer;    }; //------------------------------------------------------------------------------    /*This code is based on the performance tests at Http://jsperf.com/b64tests * This 12-bit-at-a-time algorithm was T     He best performing version on all * platforms tested. */    varB64_6bit = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; //one-dimensional arrays instead of two-dimensional arrays    varB64_12bit; //64x64 table, all axes are b64_6bit 64 characters    varB64_12bittable =function() {b64_12bit= [];  for(vari = 0; I < 64; i++) {             for(varj = 0; J < 64; J + +) {B64_12bit[i* + j] = B64_6bit[i] +B64_6bit[j]; }        }        //good idea.B64_12bittable =function() {            returnB64_12bit;        }; returnB64_12bit;    }; //RawData = Uint8array, encoded a bit like ASCII to Base64    functionuint8tobase64 (rawdata) {varNumBytes =rawdata.bytelength; varOutput = ""; varsegment; varTable =b64_12bittable ();  for(vari = 0; i < numBytes-2; i + = 3) {segment= (Rawdata[i] <<) + (Rawdata[i + 1] << 8) + Rawdata[i + 2]; Output+ = Table[segment >> 12]; Output+ = table[segment & 0XFFF]; }        if(Numbytes-i = = 2) {segment= (Rawdata[i] << + (rawdata[i + 1] << 8); Output+ = Table[segment >> 12]; Output+ = b64_6bit[(segment & 0XFFF) >> 6]; Output+ = ' = '; } Else if(Numbytes-i = = 1) {segment= (Rawdata[i] << 16); Output+ = Table[segment >> 12]; Output+ = ' = '; }        returnoutput; }});

Arraybuffer related knowledge has not been touched before, but Base64 this module feel will not be difficult to understand.

The three modules here are not dependent on the other modules, and do not interact with native.

Novice Learning Cordova-android JS Source (2)

Related Article

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.