English 中文(简体)
如何比较两个静态功能等于或不属于javascript[复制]
原标题:how compare two static function are equal or not in javascript [duplicate]
  • 时间:2009-12-20 20:39:54
  •  标签:
  • javascript

如何比较 j文中两个静态功能是否平等?

问题回答
String(f1) === String(f2)
var f1 = f2 = function( a ){ return a; };

在此,您可使用<代码>f1 == f2 <>/code>,因为它们重新标明同样的记忆,并重述同一类型。

var f1 = function( a ){ return a; },
    f2 = function( a ){ return a; };

您可在此使用所使用之星号(即暗中将功能转换成体体正文为“强权”)。

+f1 = +f2

这是ence后发生的事情:

f1.toString( ) == f2.toString( )  

www.un.org/Depts/DGACM/index_spanish.htm Edit:在一年之后回顾这一职位,我同意“@kangax”,你可能永远不会这样做。

每当我需要比较职能时 我确信,没有范围模糊,使用同样的功能目标。

Saye 我有一部分图书馆职能,这些职能作为参数之一。 为这一例子,<代码>create1分钟Callback功能将设定1分钟的计时器,并按每一计打回。 <密码>1分钟Callback> 后退,你必须像你那样通过<代码>create1分钟Callback。

function create1minuteCallback(callback){
//implementation
}

function kill1minuteCallback(callback){
//implementation
}

很显然,这不会奏效,因为我们正在通过的职能在第二行有所不同:

create1minuteCallback(function(){alert("1 minute callback is called");});
kill1minuteCallback(function(){alert("1 minute callback is called");});

我通常这样做:

function callbackFunc(){alert("1 minute callback is called");}

create1minuteCallback(callbackFunc);
kill1minuteCallback(callbackFunc);

Well, as simply as that - if you are going to compare functions, you do it for a reason I assume. What is your reason? My reason was to not run a certain function twice. I did it this way (just snippet code to get the idea)

   var x = function(){
    console.error("i am a functionX");
  }
   var y = function(){
        console.error("i am a functionX");



  }

    var z = function(){
       console.error("i am a functionZ");



  }
       var x2= x;

   var obj = new Object();
    obj[x] = "";
    obj[x2] = "";
    obj[y] = "";
    obj[z] = "";
    obj.abc = "xaxa";
                for (prop in obj) {
                    if (obj.hasOwnProperty(prop)) {
                      console.error(obj[prop] + " hello " + prop);
                    }
            }

Function x and y are the same, even though they have different whitespaces. x and y are not the same as z, since z has a different console.error. Btw open your firebug console to see it in the jsbin example





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签