Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何调用函数,例如;“新对象”;,并再次作为函数调用?_Javascript_Oop - Fatal编程技术网

Javascript 如何调用函数,例如;“新对象”;,并再次作为函数调用?

Javascript 如何调用函数,例如;“新对象”;,并再次作为函数调用?,javascript,oop,Javascript,Oop,作为学习经验,我正在尝试制作自己的面向对象的小控制台调试脚本。我希望它类似于jQuery,因为您可以将其作为函数调用(jQuery('div'))或作为对象调用(jQuery.ajax()) 我有下面的代码,这几乎是工作。它基本上是“console.log”的别名 我的目标是能够执行以下任务: var log = new RadDebug; // Create our function log('You selected: %s', fruit_type); // Invoke the top

作为学习经验,我正在尝试制作自己的面向对象的小控制台调试脚本。我希望它类似于jQuery,因为您可以将其作为函数调用(
jQuery('div')
或作为对象调用(
jQuery.ajax()

我有下面的代码,这几乎是工作。它基本上是“console.log”的别名

我的目标是能够执行以下任务:

var log = new RadDebug;
// Create our function

log('You selected: %s', fruit_type);
// Invoke the top-level function
// Output: You selected: "Apple"

log.warn('You selected: %s', fruit_type);
// Invoke a method "warn", which displays a caution icon next to the log.
// Output: (!) You selected "Apple"

我正在编写的脚本:

function RadDebug() {
  var debug_enabled = (debug_mode && window.console && window.console.log instanceof Function);

  // If this object was already initialize, redirect the function call to the ".log()" as default
  if ( this.initialized ) {
    return this.log.apply( this, arguments );
  }
  this.initialized = true;

  this.log = function() {
    if ( !debug_enabled ) return;
    console.log.apply( console, arguments );
  };

  this.info = function() {
    if ( !debug_enabled ) return;
    console.info.apply( console, arguments );
  };

  // More methods below
}

问题是: 调用
log.warn(“hello world”)
的工作与您预期的一样

调用
log(“helloworld”)
告诉我
TypeError:Object不是函数

问题:如何使其作为函数工作并具有类似对象的属性?(比如jQuery是如何做到的)


(多亏了@FelixKling,这个问题已经解决了。最后的代码是您是否想检查它)。

不要将
RadDebug
用作构造函数,只需将方法附加到函数本身即可

例如:

var RadDebug = (function() {
  // Some IIFE to keep debug_enabled and functions local
  var debug_enabled = (debug_mode && window.console && window.console.log instanceof Function);

  // Functions here
  function log() {
    if ( !debug_enabled ) return;
    console.log.apply( console, arguments );
  }

  function info() {
    if ( !debug_enabled ) return;
    console.info.apply( console, arguments );
  }

  // ...

  // Attach methods to "main" log function
  log.log = log;
  log.info = info;
  // ...

  // Return log function (RadDebug === log)
  return log;
}());
var hello = function () {
  var fn = function () {
    console.log("Hello");
  }

  fn.hello = fn;

  return fn;
}

var sayHello = new hello();
sayHello(); // prints "Hello"
sayHello.hello(); // prints "Hello"
然后你把它当作

RadDebug('You selected: %s', fruit_type);
// same as
RadDebug.log('You selected: %s', fruit_type);

RadDebug.info('You selected: %s', fruit_type);

或者将
RadDebug
别名为您想要的任何对象(例如
log
)。

函数是Javascript中的对象。您可以在
RadDebug
函数中返回函数,并将其一个成员设置为相同的返回函数。例如:

var RadDebug = (function() {
  // Some IIFE to keep debug_enabled and functions local
  var debug_enabled = (debug_mode && window.console && window.console.log instanceof Function);

  // Functions here
  function log() {
    if ( !debug_enabled ) return;
    console.log.apply( console, arguments );
  }

  function info() {
    if ( !debug_enabled ) return;
    console.info.apply( console, arguments );
  }

  // ...

  // Attach methods to "main" log function
  log.log = log;
  log.info = info;
  // ...

  // Return log function (RadDebug === log)
  return log;
}());
var hello = function () {
  var fn = function () {
    console.log("Hello");
  }

  fn.hello = fn;

  return fn;
}

var sayHello = new hello();
sayHello(); // prints "Hello"
sayHello.hello(); // prints "Hello"

每个函数在JavaScript中都是一个对象,我知道这一点,但问题是
log()
不再可用作函数-只能用作对象(例如
log.info()
)。对于这个小项目,这是我唯一无法解决的问题。^不要将
RadDebug
用作构造函数,只需将方法附加到函数本身即可。@FelixKling我尝试过,
var log=function(){…}
。它解决了
log()
问题。但是我不能使用
log.info()
。我想要“两全其美”。我想你错过了@Felix Kling提到的第二部分,“只需将方法附加到函数本身”是的,就是这样!非常有趣的方法。这将需要更多的时间来了解这一切是如何运作的,但它似乎相当圆滑。匿名函数正在构建“log”函数并向其附加方法。那样看起来很干净。非常感谢。这只是一个很好的答案。它解释了很多关于javascript的方法。我希望我能投更多的票。