当前位置: 代码迷 >> JavaScript >> javascript权威指南 源代码分析(2)Functions
  详细解决方案

javascript权威指南 源代码分析(2)Functions

热度:443   发布时间:2012-08-09 15:59:21.0
javascript权威指南 源代码分析(二)Functions

javascript权威指南 源代码分析(二)Functions
2012年01月02日
  function classof(o) {     if (o === null) return "Null";     if (o === undefined) return "Undefined";     return Object.prototype.toString.call(o); } -------------------------------------------------- --------------------------------------------------- --------- Functions 定义:
  ------------------------------------
  function factorial(x) {
  if (x ReferenceError: fact is not defined
  f(9); //362880
  ------------------------------------
  //data.sort(function(a,b) { return a-b; });
  Array.sort(function(a,b) { return a-b; });
  [1994,1976].sort();
  //[1976, 1994] //"[object Function]"
  ------------------------------------
  var tensquared = (function(x) {return x*x;}(10)); classof(tensquared); //"[object Number]" typeof tensquared  === "number"; // true ------------------------------------ var operators = {     num:      3.14,     add:      function(x,y) { return x+y; },     subtract: function(x,y) { return x-y; },     multiply: function(x,y) { return x*y; },     divide:   function(x,y) { return x/y; },     pow:      Math.pow  // Works for predefined functions too }; classof(operators); //"[object Object]" classof(operators.add); //"[object Function]" classof(operators.num);//"[object Number]" typeof operators[pow] === "function"//ReferenceError: pow is not defined typeof operators["pow"] === "function"//  true ====================== var o =function() {}; typeof o === "function" var o =Object.create; typeof o === "function" ------------------------------- var o =Object(); typeof o === "object" var o = []; typeof o === "object" var o = Array(); typeof o === "object" ------------------------------- var o ={}; typeof o === "object" ========================== -------------------------------------------------- --------------------------------------------------- -------------------------- Example 8-3(当前) demonstrates this namespace technique. It defines an anonymous function that returns an extend() function like the one shown in Example 6-2(p152).      https://developer.mozilla.org/en/JavaScript/Refere nce/Global_Objects/Object/defineProperty  https://developer.mozilla.org/en/JavaScript/Refere nce/Global_Objects/Object/getOwnPropertyDescriptor -------------------------------------------------- ------ Object.defineProperty(Object.prototype,   //Example 6-2(p152).      "extend",                  // Define Object.prototype.extend     {         writable: true,         enumerable: false,     // Make it nonenumerable         configurable: true,         value: function(o) {               var names = Object.getOwnPropertyNames(o);             for(var i = 0; i Error("set" + name + ": invalid value " + v);         else             value = v;     }; }  var o = {};  // Here is an empty object addPrivateProperty(o, "Name", function(x) { return typeof x == "string"; }); o.setName("Frank");       // Set the property value console.log(o.getName()); // Get the property value o.setName(0);             // Try to set a value of the wrong type -------------------------------------------------- --------------------------------------------------- -------  if (!Function.prototype.bind) {//Function.prototype.bind     Function.prototype.bind = function(o /*, args */) {         var self = this, boundArgs = arguments; //保存this指针和原有参数
  return function() {
  var args = [], i;
  for(i = 1; i f.call(o, 1, 2);
  g(3)=> f.call(o, 1, 2, 3);
  -------------------------------------------------- --------------------------------------------------- ------- 
  相关解决方案