简体   繁体   English

通过变量重新初始化javascript函数对象

[英]Re-initialize a javascript function object by variable

This one is something that is fairly easy to do in PHP and I find my self in a situation where it would come in handy, but I do not believe the PHP trick will work. 这是在PHP中很容易做到的事情,我发现自己会很方便,但是我认为PHP技巧不会奏效。

Basically I want to use a variable passed from a function within an object to then reinitialize that object using the child (defined by the variable). 基本上,我想使用从对象中的函数传递的变量,然后使用子对象(由变量定义)重新初始化该对象。

var View = function(){
    var fn = this;

    fn.load = function(name){
        return new name();
    }
}
var view = View.load('titleView');

This is a very early work on it, so forgive the fact that it looks so strange (still need to tinker more with the concept). 这是一项非常早期的工作,因此请原谅它看起来如此奇怪(仍然需要对该概念进行更多修改)。 But overall it should roughly show the concept. 但总体而言,它应该大致显示该概念。

Is there a way to basically recreate the current functions instance with a new function? 有没有一种方法可以用新功能基本上重新创建当前功能实例? To do this in the aspect I am thinking of I will need to use a variable rather then pass the new object. 为此,我正在考虑要使用变量而不是传递新对象。 Is this possible? 这可能吗? I am sure in some form it has to be. 我确信它一定是某种形式。 Any ideas/pointers? 有任何想法/指标吗? Google has been failing me since I am not sure of the right keywords for this. Google一直让我失望,因为我不确定关键字是否合适。

EDIT: should also show the idea behind the "titleView" class 编辑:还应该显示“ titleView”类背后的想法

var titleView = function(){}

titleView.prototype = new View;

I think the easiest way to do this is via some kind of factory that can produce the types of views you are wanting. 我认为最简单的方法是通过某种工厂来生成所需的视图类型。 Something like this: 像这样:

var View = (function() {
    var registry = {};

    return {
        register: function(type, fn) {
            if (typeof registry[type] === 'undefined') {
                registry[type] = fn;
                return true;
            }
            return false;
        },

        load: function(type) {
            return new registry[type]();
        }
    };
})();

var titleView = function() {
    this.name = 'titleView';
};
var subTitleView = function() {
    this.name = 'subTitleView';
}

View.register('titleView', titleView);
View.register('subTitleView', subTitleView);

var view = View.load('titleView');
console.log("Created view type: " + view.name);

view = View.load('subTitleView');
console.log("Created view type: " + view.name);

This would give the following ( allowing you to recreate view variable on the fly ): 这将提供以下内容( 允许您即时重新创建view变量 ):

// Created view type: titleView
// Created view type: subTitleView

If you try to do it the way your example is going, you'll have to use subclasses like so: 如果尝试按照示例的方式进行操作,则必须使用如下子类:

function Base() {
    this.name = "Base";
    this.load = function(fn) {
        fn.apply(this);
    }
}
function Other() {
    Base.apply(this, arguments);
    this.name = "Other";
}

var view = new Base();
console.log(view);

view.load(Other);
console.log(view);

// => Base { name: "Base", load: function }
// => Base { name: "Other", load: function }

However, with this method, after calling view.load(Other) , your view will still retain whatever properties/methods it had prior to calling load ( which may not be what you want ). 但是,使用此方法,在调用view.load(Other) ,您的视图仍将保留调用load之前的所有属性/方法( 可能不是您想要的 )。

think this is what ur asking 认为这是你问的

var View = function(){
  this.load = function(name){
  return  name;
  }
}
var myView = new View;
var v = myView.load('titleView');
alert(v);

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

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