Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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 - Fatal编程技术网

Javascript 如何将普通函数包装到构造函数中?

Javascript 如何将普通函数包装到构造函数中?,javascript,Javascript,是否可以创建一个包装器函数MyFunction,当使用new调用它时,如中所示 是否真的返回相同的对象,就好像callsite调用了SomeOtherFunction而没有使用new 我已经看过了,但看起来Chrome还不支持它们 编辑: 结果callsite调用MyFunction的方式如下: var instance = Object.create(MyFunction.prototype); MyFunction.apply(instance, [/* arguments */]); //

是否可以创建一个包装器函数MyFunction,当使用new调用它时,如中所示

是否真的返回相同的对象,就好像callsite调用了SomeOtherFunction而没有使用new

我已经看过了,但看起来Chrome还不支持它们

编辑: 结果callsite调用MyFunction的方式如下:

var instance = Object.create(MyFunction.prototype);
MyFunction.apply(instance, [/* arguments */]);
// `instance` is supposed to be initialized here

我想这就是你要找的?注意,正如Jan Dvorak提到的,您只能返回对象

function SomeObject() {
    return Construct();
}

function Construct() {
    return { 'name' : 'yessirrreee' };
}

console.log(new SomeObject())

你可以试试这样的

function MyClass() {
}

var ob = new MyClass();
评论后编辑 我认为这个问题需要更多的背景。 我建议你读一下,但在你更好地弄清楚你到底想完成什么之前,我帮不了你

我真的不知道你的代码是什么样子,但这里有一些建议 不管怎样。我猜nr2就是你想要的:

// 1
function MyFunction() {
  this.__proto__ = SomeOtherFunction()
}

function SomeOtherFunction() {
  return {
    foo: function() {
      return 'bar'
    }
  }
}

var fn = new MyFunction()
fn.foo()

// 2
function MyFunction() {}
MyFunction.prototype = SomeOtherFunction()

function SomeOtherFunction() {
  return {
    foo: function() {
      return 'bar'
    }
  }
}

var fn = new MyFunction()
fn.foo()

// 3
function MyFunction() {}
MyFunction.prototype = SomeOtherFunction.prototype

function SomeOtherFunction() {
  function Foo() {}
  Foo.prototype.foo = function() {
    return 'bar'
  }
}

var fn = new MyFunction()
fn.foo()

当然:函数MyFunction{return SomeOtherFunction;}@FelixKling我已经试过了,新的MyFunction返回的是一个空对象,它与SomeOtherFunction没有相同的属性。但是为什么要这样做呢?@mksio不太可能。其他函数的返回值是对象吗?构造函数只能返回对象。@调用站点上的ArnarYngvason(我无法修改)需要一个构造函数。SomeOtherFunction是一个返回新对象的普通函数,我也不能修改它。我想把两者联系起来。是的,在这个例子中,我可以确认对象正在被传递,但是在我这边挖掘了一点之后,我发现调用站点调用构造函数的方式有点不同。参见编辑
function MyClass() {
}

var ob = new MyClass();
// 1
function MyFunction() {
  this.__proto__ = SomeOtherFunction()
}

function SomeOtherFunction() {
  return {
    foo: function() {
      return 'bar'
    }
  }
}

var fn = new MyFunction()
fn.foo()

// 2
function MyFunction() {}
MyFunction.prototype = SomeOtherFunction()

function SomeOtherFunction() {
  return {
    foo: function() {
      return 'bar'
    }
  }
}

var fn = new MyFunction()
fn.foo()

// 3
function MyFunction() {}
MyFunction.prototype = SomeOtherFunction.prototype

function SomeOtherFunction() {
  function Foo() {}
  Foo.prototype.foo = function() {
    return 'bar'
  }
}

var fn = new MyFunction()
fn.foo()