小编ren*_*846的帖子

Haxe,如何检查param是否为Array类型

我正在将一个JavaScript库转换为Haxe.似乎Haxe与JS非常相似,但在工作中我遇到了覆盖函数的问题.

例如,在下面的函数param可以是整数或数组.

使用Javascript:

function testFn(param) {
    if (param.constructor.name == 'Array') {
        console.log('param is Array');
        // to do something for Array value
    } else if (typeof param === 'number') {
        console.log('param is Integer');
        // to do something for Integer value
    } else {
        console.log('unknown type');
    }
}
Run Code Online (Sandbox Code Playgroud)

HAXE:

function testFn(param: Dynamic) {
    if (Type.typeof(param) == 'Array') { // need the checking here
        trace('param is Array');
        // to do something for Array value
    } else if (Type.typeof(param) == TInt) { …
Run Code Online (Sandbox Code Playgroud)

arrays haxe

5
推荐指数
1
解决办法
703
查看次数

如何在Haxe中克隆动态对象?

我有一个来自Json的Dynamic对象,需要在Haxe中克隆它.有没有简单的克隆对象的方法,请告诉我.或者如果不可能,我想至少迭代那个动态对象,比如JavaScript对象.

var config = {
    loop : true,
    autoplay : true,
    path : "data.txt"
};
var newConfig = {};
for (i in config) {
    if (config.hasOwnProperty(i))
        newConfig[i] = config[i];
}
Run Code Online (Sandbox Code Playgroud)

clone haxe dynamic

5
推荐指数
1
解决办法
713
查看次数

带有 Symbol 键的 JavaScript 对象被 Stringify 删除,也无法迭代

我正在使用 Sequelize orm 模块。这是一个很棒的 orm 模块。但在 where 选项上有一些问题。

const option = { where: { name: { [Op.like]: `%${name}%` } } }
const result = await model.findOne(option)
Run Code Online (Sandbox Code Playgroud)

正如您在上面的代码中看到的,该name属性以对象Op.like作为键。我检查了 Sequelize 的代码库Op,发现它是Symbol. 这Op.like只是Symbol.for('like')

当然,使用orm本身没有问题,但我的问题在于将optionas json字符串化。结果Json.stringify如下(Symbol 键值被删除):

{"where":{"name":{}}}
Run Code Online (Sandbox Code Playgroud)

我必须保存所有where查询历史记录并稍后重用,但没有任何解决方案。研究了许多 stringify 库,例如circular-jsonjson-stringify-safe,但仍然存在同样的问题。也读过这个问题,但这是使用符号的情况value,而不是key。并尝试使用自定义替换器,但也无法迭代。

JSON.stringify(option, (key, value) => {
  if(typeof key === 'symbol') {
    console.log('symbol key: ', key) // …
Run Code Online (Sandbox Code Playgroud)

javascript json

3
推荐指数
1
解决办法
856
查看次数

如何访问 SwiftUI 中的子视图?

我正在研究 SwiftUI,感觉它与 React 非常相似。刚才我正在自定义 SwiftUI 的 Button 并且遇到了无法动态访问 Button 的子视图的问题 以下代码是我要做的:

struct FullButton : View {
  var action: () -> Void
  var body: some View {
    Button(action: action) {
      // render children views here even what is that
      children
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

和用法:

VStack {
  FullButton(action: {
    print('touched')
  }) {
    Text("Button")
  }
}
Run Code Online (Sandbox Code Playgroud)

请问,我有什么错误的想法吗?


更新

取决于@graycampbell 的回答我尝试如下

struct FullButton<Label> where Label : View {
    var action: () -> Void
    var label: () -> Label

    init(action: @escaping () -> Void, @ViewBuilder …
Run Code Online (Sandbox Code Playgroud)

swiftui

3
推荐指数
1
解决办法
3319
查看次数

正则表达式匹配2位数但不同的数字

我最近几天正在使用正则表达式,现在需要制作与2位数匹配的正则表达式但数字应该彼此不同例如以下匹配:56,78,20 ......但是以下不应该匹配: 22,33,66或99

这个解决方案已经浪费了几天时间.所以任何建议都会受到欢迎.

regex

2
推荐指数
1
解决办法
54
查看次数

标签 统计

haxe ×2

arrays ×1

clone ×1

dynamic ×1

javascript ×1

json ×1

regex ×1

swiftui ×1