日期:2014-05-16  浏览次数:20395 次

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