简体   繁体   English

我真的需要在javascript中为此使用原型吗? (实际示例)

[英]Do I really need to use protoypes for this in javascript? (Practical example)

Can this be written without complexing things with prototypes? 是否可以编写这些原型而不会使原型复杂化?

Why? 为什么? The current code does what I want, but it bothers me how trixy it is to follow and how error prone it is, also seems to be performance wasting since things are duplicated. 当前的代码可以实现我想要的功能,但是它困扰着我要遵循的技巧有多复杂,错误有多容易,而且由于重复操作,似乎也浪费了性能。

Aim? 目标? The more I spend using prototype and this I get the sense the code would be simpler and more to the point if this was not the case. 我花了更多的时间使用原型,这使我觉得代码不是那么简单,而且更有意义。

Especially if the this-functions in SystemBlueprint can be rewritten to take an instance as argument instead. 尤其是如果可以重写SystemBlueprint中的this-functions来以实例作为参数。 And if object Function Log() and Out could just be plain objects somehow? 如果对象Function Log()和Out可能以某种方式只是普通对象? How can Log or Out be extracted outside of SystemBuilder? 如何在SystemBuilder外部提取Log或Out?

Full code in Jsbin Jsbin中的完整代码

https://jsbin.com/pigosijaxo/edit?js,console (Updated) https://jsbin.com/pigosijaxo/edit?js,控制台 (已更新)

// Local for each System object
var SystemData = {
    name: '?',
    id: 1,
    actions: [],
    destinations: []
}

// Variables shared among all Systems
const SystemShare = {
    global: 1
}

// this-Functions shared among all Systems
function SystemBlueprint() {}
SystemBlueprint.prototype = {
    run() {
        var length = this.actions.length
        for (var i = 0; i < length; i++) {
            var result = this.actions[i](arguments, this)
            if (result && this.destinations.length > 0) {
                for (var n = 0; n < this.destinations.length; n++) {
                    this.destinations[n].call(null, result)
                }
            }
        }
    },
    does(algorithm) {
        this.actions.push(algorithm)
        return this
    },
    random(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
}

function SystemBuilder(name) {
    // copy shared methods
    var system = Object.create(SystemBlueprint.prototype)
    Object.assign(system, JSON.parse(JSON.stringify(SystemData))) //deep copy

    system.name = name
    system.id = SystemShare.global++

    function Log() {}
    Log.prototype.local = () => console.log('fields: ' + JSON.stringify(Object.keys(system))),
    system.log = new Log()

    function Out(){}
    Out.prototype.into = (destination) => {
        system.destinations.push(destination)
        return system
    }
    system.out = new Out()

    system.trigger = {}
    function OnEvent(trigger){
        if(trigger === undefined) return
        trigger.call(null, system.run.bind(system))
        return system
    }
    system.trigger.on = new OnEvent()
    return system
}

var system = new SystemBuilder()
system.my = 'Testing'
system.log.local()
system.does( () => 'printing output...')
system.out.into(console.log)
system.run()

Partial Answer, implementation from comment suggestion by @Bellian, a bit on the way for sure, thanks! 部分答案,来自@Bellian的评论建议的实现,可以肯定,谢谢!

Where? 哪里? Inside function SystemBuilder(...): 内部函数SystemBuilder(...):

Instead of 代替

function Log() {}
Log.prototype.local = () => console.log('fields: ' + JSON.stringify(Object.keys(system))),
system.log = new Log() 

Do this 做这个

function _local(system){
  console.log('fields: ' + JSON.stringify(Object.keys(system)))
}
system.log = {local: _local.bind(this, system)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM