我正在尝试在 ES6 中实现命名构造函数的使用。这样做的原因是我认为避免使用new关键字调用构造函数,而是使用类的简单方法来使用其他方法会更令人愉快。我想为此使用静态函数作为Proxy构造函数。
我尝试了以下方法:
\n\nclass Person {\r\n constructor(...props) {\r\n let {name, age} = props;\r\n this.name = name;\r\n this.age = age;\r\n }\r\n static create(...props) {\r\n return new Person(props);\r\n }\r\n \r\n display() {\r\n console.log(this)\r\n }\r\n}\r\n\r\nPerson.create({name: \'John\', age: 28}).display(); //Simple object inputRun Code Online (Sandbox Code Playgroud)\r\n但这不会起作用,因为简单的对象输入给出:
\n\nPerson\xc2\xa0{name: undefined, age: undefined}\nRun Code Online (Sandbox Code Playgroud)\n\n任何帮助,将不胜感激。
\n\n更新:谢谢,@appleapple 的回答很有帮助。我没有注意到我正在传递一个参数。对于那些想知道如何为 n-Ary 构造函数方法完成此操作的人(当然使用对象很简洁,但仍然如此),这里有一个示例:
\n\nPerson\xc2\xa0{name: undefined, age: undefined}\nRun Code Online (Sandbox Code Playgroud)\r\n在解决问题期间,我遇到了一个代码问题:
var a = {};
a: {m: "something"; n: "another thing";}Run Code Online (Sandbox Code Playgroud)
现在,如果我想访问m还是n在这里,我应该怎么办?在实践中可以在哪里使用这种代码?
我最近注意到 App.js 中没有用于 react 的 import 语句,例如:
import React from 'react'
Run Code Online (Sandbox Code Playgroud)
在所有教程中,我都看到了这一行。现在是自动的吗?
如果我function使用Function构造函数创建一个新的,我怎么能给它一个非临时范围来访问window(意味着范围只需要评估一次,而不是每次调用函数)?目的是构造需要一些非常昂贵的计算的多个变量,并且我不想在每次调用函数时重构它们,但我也不想将它们存储在中window.有任何想法吗?
我正在尝试为选项参数定义具有以下行为的构造函数:
class Test {
constructor({
someOption = 'foo',
otherOption = 'bar',
aSeedOfSomeSort = Math.random(),
// ...
}) {
console.log(
someOption, otherOption, aSeedOfSomeSort
);
}
}
new Test({someOption: 'lol'});
new Test({otherOption: 'derp'});
new Test({aSeedOfSomeSort: 0.5});
new Test({});
new Test(); // here's the problemRun Code Online (Sandbox Code Playgroud)
这很好用,但是,我希望构造函数能够工作,以便使用所有默认参数,我不必传递空对象。我不关心对象本身是否在参数中命名,但在构造函数的上下文中,我想要一种干净的方法来直接访问所有选项,而无需命名空间或使用with.
这可能吗?
我想创建一个多维对象.以下是我的示例代码.我对JavaScript不太熟悉.
示例代码
var test = {};
test[0] = {1:{a:1,b:2,c:3}};
if(true)
{
test[0] = {2: {c:1,b:2,a:3}};
}
console.log(test);
Run Code Online (Sandbox Code Playgroud)
期待结果
{
0: {
1:{a:1,b:2,c:3},
2:{c:1,b:2,a:3}
}
}
Run Code Online (Sandbox Code Playgroud) 我是系统安全的新手,我正在尝试实现一个简单的返回libc漏洞.有了GCC的-fno-stack-protector设置,现在我知道如何利用缓冲区溢出错误来粉碎返回地址.现在我想system()通过覆盖函数的正确返回地址来引导目标程序的控制流程到C 函数system()(我用-static选项集编译程序,以便所有C的标准函数都在可执行代码中) .例如,目标程序如下:
int main(int argc, char *argv[]) {
char buffer[8];
gets(buffer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以main()使用system()gdb的反汇编函数找到的地址覆盖返回地址.我想提供"/ bin/sh"作为参数system(),但我不知道system()函数的参数在地址空间中的位置.任何人都可以帮我弄清楚我能在哪里找到这个论点?
我尝试用扩展语法编写三元运算符并复制两个对象.是否可以在文字对象中使用带有扩展语法的三元运算符?我的代码工作正常,我只想优化它.
hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint,
Run Code Online (Sandbox Code Playgroud)
我想这样写:
hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}},
Run Code Online (Sandbox Code Playgroud) 我有这个代码。
const elements = [...document.querySelectorAll('.someClass')]
Run Code Online (Sandbox Code Playgroud)
的组合:
我不知道如何将 Typescript 类型添加到这一行。
我从控制台收到错误:
TS2548: Type 'NodeListOf<Element>' is not an array type or does not have a '[Symbol.iterator]()' method that returns an iterator.
Run Code Online (Sandbox Code Playgroud) 使用引用子类类型的 switch case 的方法创建一个基类。
下面的一段代码是否违反了面向对象原则,如果是,是哪一个?
public abstract class BaseClass
{
public BaseClass Method()
{
switch (this)
{
case DerivedClass1 s:
return new DerivedClass1();
case DerivedClass2 c:
return new DerivedClass2();
case DerivedClass3 r:
return new DerivedClass3();
default:
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×7
ecmascript-6 ×2
assembly ×1
c# ×1
constructor ×1
es6-class ×1
function ×1
jsx ×1
object ×1
oop ×1
reactjs ×1
scope ×1
typescript ×1
x86 ×1