Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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,我希望能够通过创建一个对象并传递构造函数来连接字符串,因此每次调用它时都会保留前一个对象的引用。我想实现这样的目标: foo = Chain("h") bar = foo("e")("l")("l")("o") foo.toString() == "h" bar.toString() == "hello" bar.anc

我希望能够通过创建一个对象并传递构造函数来连接字符串,因此每次调用它时都会保留前一个对象的引用。我想实现这样的目标:

foo = Chain("h")
bar = foo("e")("l")("l")("o")

foo.toString()          == "h"
bar.toString()          == "hello"
bar.ancestor.toString() == "hell"
到目前为止,我所拥有的是一个方法链接,虽然它非常相似,但并不完全是我想要在这里实现的。我一直在遵循以下文档:

函数ChainParent(){
}
功能链(字母){
这个字母=字母;
this.祖先=this.letter.substring(0,this.letter.length-1);
}
Chain.prototype=Object.create(ChainParent.prototype)
Chain.prototype.constructor=Chain;
Chain.prototype.create=函数create(字母){
const-letra=此.letter.concat(字母);
返回新的this.constructor(letra)
}
Chain.prototype.toString=函数(){
console.log(这封信);
}
常量foo=新链(“h”);
const bar=foo.create(“e”).create(“l”).create(“l”).create(“o”);
foo.toString();
bar.toString();

bar.祖先.toString()//这不起作用
这里有一个可能的
链实现
-

const chain=(s=”“)=>
Object.assign
(下一步=>链(s+next)
,{toString:=>s}
,{祖先:{=>chain(s.slice(0,-1))}
)
常量foo=链(“h”)
常数bar=foo(“e”)(“l”)(“l”)(“o”)
console.log(foo.toString())
console.log(bar.toString())
console.log(bar.祖先().toString())

console.log(bar.concenter().concenter().toString())
下面是
链的一个可能实现-

const chain=(s=”“)=>
Object.assign
(下一步=>链(s+next)
,{toString:=>s}
,{祖先:{=>chain(s.slice(0,-1))}
)
常量foo=链(“h”)
常数bar=foo(“e”)(“l”)(“l”)(“o”)
console.log(foo.toString())
console.log(bar.toString())
console.log(bar.祖先().toString())

console.log(bar.祖先().祖先().toString())
您应该将祖先设置为当前实例(
this

请注意,
toString
是一种特殊的方法,因为当JavaScript引擎需要将实例转换为原语值时,会自动调用它。因此,您应该坚持使用它的“接口”:它应该返回字符串(而不是输出它)


下面是使用现代
语法实现的最后一个块。我允许
字母
实际上是一个包含多个字符的字符串。
祖先
属性将返回最新扩展名之前的字符串:

类链{
构造函数(字母,祖先=null){
这个字母=字母;
这个。祖先=祖先;
}
创建(字母){
返回新链(this.letter+letter,this);
}
toString(){
回信;
}
}
常量foo=新链(“h”);
const bar=foo.create(“e”).create(“l”).create(“l”).create(“o”);
log(foo.toString());
console.log(bar.toString());

console.log(bar.祖先.toString())您只需将祖先设置为当前实例(
this

请注意,
toString
是一种特殊的方法,因为当JavaScript引擎需要将实例转换为原语值时,会自动调用它。因此,您应该坚持使用它的“接口”:它应该返回字符串(而不是输出它)


下面是使用现代
语法实现的最后一个块。我允许
字母
实际上是一个包含多个字符的字符串。
祖先
属性将返回最新扩展名之前的字符串:

类链{
构造函数(字母,祖先=null){
这个字母=字母;
这个。祖先=祖先;
}
创建(字母){
返回新链(this.letter+letter,this);
}
toString(){
回信;
}
}
常量foo=新链(“h”);
const bar=foo.create(“e”).create(“l”).create(“l”).create(“o”);
log(foo.toString());
console.log(bar.toString());
console.log(bar.祖先.toString())这里有一个用于使类可调用的解决方案,这样链接的样板文件就少了

在本例中,类的每个实例都保留了给定的起始字母,然后在
toString
函数中使用递归将它们合并在一起。因此,它充当一个链接列表,仅引用父对象

//https://stackoverflow.com/a/36871498/13175138
类ExtensibleFunction扩展函数{
建造师(f){
返回Object.setPrototypeOf(f,new.target.prototype);
}
}
类链扩展了可扩展函数{
构造函数(字母,链=null){
超级((字母)=>新链(字母,本));
这个祖先=链;
这个字母=字母;
}
toString(){
如果(!this.祖先){
回信;
}
返回this.祖先.toString()+this.letter;
}
}
常量foo=新链('h');
常数bar=foo('e')('l')('l')('o');
log(foo.toString());
console.log(bar.toString());
console.log(bar.祖先.toString());
const foobar=新链(“hel”);
const barfoo=foobar('ll')('o');
log(foobar.toString());
log(barfoo.toString());
log(barfoo.祖先.toString())这里有一个用于使类可调用的解决方案,这样链接的样板文件就少了

在本例中,类的每个实例都保留了给定的起始字母,然后在
toString
函数中使用递归将它们合并在一起。因此,它充当一个链接列表,仅引用父对象

//https://stackoverflow.com/a/36871498/13175138
类ExtensibleFunction扩展函数{
建造师(f){
返回Object.setPrototypeOf(f,new.target.prototype);
}
}
类链扩展了可扩展函数{
构造函数(字母,链=null){
超级((