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);
  -------------------------------------------------- --------------------------------------------------- -------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值