2014-02-26 51 views
2

所以我有一个对象对象用一些方法。如果没有任何方法调用该对象,我想运行一个特定的方法。那可能吗?如果是这样如何?当调用对象时没有方法的JS调用函数

例如

function obj(variable){ 
    this.method = function(){}; 
    this.noMethod = function(){}; 
} 
var test = new obj(variable); 

调用console.log(test)现在应该在测试中调用noMethod。 我希望你明白我的意思。

该对象有一个nodelist,如果你调用一个方法,它会运行该方法。 如果你只是调用对象,它应该返回节点列表并可能改变它,这取决于参数。

有没有办法检查对象是否用方法调用?这样我可以有一个if语句来检查并运行我的方法,如果没有其他方法被调用。

+0

也许ü应该考虑代理http://wiki.ecmascript.org/ doku.php?id = harmony:direct_proxies –

+0

嗨Lukas,你的问题的背景是什么?你正在寻找一种方式来查看控制台中的细节? –

+0

我应该多解释一下,该对象有一个nodelist。我想要运行一个方法或返回节点列表。 –

回答

0

这应该调用没有方法时使用,并调用一个方法,如果不是那么我不知道你可以做到这一点,没有一个函数。

function Obj() { 
    this.method = function(){}; 
    this.noMethod = function(){}; 
    return this.noMethod(); 
} 

我最初说要使用参数var,但仍然不符合您的要求。

+0

这个问题,因为我最初发布类似 - 是在他的obj,他会调用方法,并调用no方法..因为test.method()也调用没有参数的obj。 –

+0

使用“新”运算符通常引用“构造函数设计模式” 为了避免误解代码样式/约定,请简单查看构造函数模式 http://addyosmani.com/resources/essentialjsdesignpatterns/ book /#constructorpatternjavascript – dsuess

+0

这只有在我永远不会用参数初始化obj时才有效,对吧?我也需要这一点,在上面调整。 –

1

你看着及arguments.length为JavaScript

function obj(variable, bool){ 
    if(bool) 
    this.method = function(){}; 
    else 
    this.noMethod = function(){}; 
} 
var test = new obj(something, false); 

调用console.log(test)应导致this.noMethod

+0

你的例子是倒退的,但是有效的。 – Mouseroot

+0

我认为这在执行新对象('some value')时失败;因为它不会是0. –

+0

@Mouseroot非常细心,谢谢! – mrpanda

0

您正在寻找此

function obj(){ 
    this.method = function(){}; 
    this.noMethod = function(){}; 
    this.toString = function() {return 'x'} 
} 
var test = new obj(); 
alert(test) 

或本:

function obj(){ 
    this.method = function(){}; 
    this.noMethod = function(){}; 
} 
obj.prototype.toString = function() {return 'x'} 
var test = new obj(); 
alert(test) 

查看更多在这里:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

+1

但是,这只适用于我需要它在字符串上下文。如果我做console.log(测试)它不会调用toString,对吗? (多数民众赞成在我尝试它时发生了什么) –

+0

正确。 Console.log是它自己的一头野兽。 AFAIK,它只是调用toString来迭代项目属性。 –

1

注意你想要的确切行为是不可能的(据我所知),因为

  • 要拨打test.noMethod无需转换test成字符串,也使其成为一个函数调用它。这意味着你需要一个吸气。
  • 但是,如果您将吸气剂添加到test,那么test.otherMethod也会拨打test.noMethod

一些替代:

  • 使用.toString()方法:

    function Obj(variable){ 
        this.method = function(){}; 
        this.noMethod = function(){ alert('foo'); }; 
        this.toString = function(){ return this.noMethod(); } 
    } 
    var test = new Obj(); 
    test+''; // Calls test.noMethod, and returns test.noMethod() 
    
  • 使用吸气剂(1):

    function Obj(variable){ 
        this.method = function(){}; 
        this.noMethod = function(){ alert('foo'); }; 
    } 
    var obj = {test: new Obj()}, tmp = obj.test; 
    Object.defineProperty(obj, 'test', { 
        get: function(){ return tmp.noMethod() } 
    }); 
    obj.test; // Calls tmp.noMethod, and returns test.noMethod() 
    obj.test.noMethod(); // Calls test.noMethod once and throws an error! 
    
  • 使用吸气剂(2):

    function Obj(variable){ 
        this.method = function(){}; 
        this.noMethod = function(){ alert('foo'); }; 
    } 
    var obj = {test: new Obj()}, tmp = obj.test; 
    Object.defineProperty(obj, 'test', { 
        get: function(){ tmp.noMethod(); return tmp } 
    }); 
    obj.test; // Calls tmp.noMethod, and returns tmp 
    obj.test.noMethod(); // Calls test.noMethod twice! 
    
  • 使用功能:

    function Obj(variable){ 
        var f = function(){ return f.noMethod(); }; 
        f.method = function(){}; 
        f.noMethod = function(){ alert('foo'); }; 
        return f; 
    } 
    var test = new Obj(); 
    test(); // Calls test.noMethod, and returns test.noMethod() 
    test instanceof Obj // false! 
    
0

从对这个问题您的意见,这并不完全解决你是什么之后,我不知道如果这是真的有可能,因为我认为你试图在不调用函数的情况下执行一个函数 - 这对于构造函数的想法是可能的 - 但我不认为你在这之后。

但是,这种结构可能会给你一些替代方案的想法(尽管承认这不是你真正想要的)。

var test = function() { 
    alert('hello'); 

    return { 
     moo : function() { 
      alert('moo'); 
     } 
    } 
}; 

var obj = test(); //hello 

obj.moo(); //moo 

http://jsfiddle.net/6Vpfa/


编辑:

也许它确实帮助,幸福的日子:-)

+0

好吧,它看起来非常像我需要的东西,你为什么说它不是我所需要的? –

+0

酷!很幸运!我之所以这么认为的原因是,test()的行为类似于构造函数,应该只执行一次,因为它每次都会返回一个新的“moo函数”。但你可能会扭曲这个以适应你的需求。 –

+0

补偿我认为它确实不起作用。因为如果你需要返回节点列表,你会遇到麻烦,它只能用于alert,因为它会自动调用。 –

相关问题