javascript instantiation and arguments.callee

本文详细解释了构造函数与普通函数的区别,并提供了防止误调用构造函数导致意外覆盖变量值的方法。通过实例演示了如何利用 arguments.callee 巧妙地确保构造函数被正确调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

in the nutshell, it is no difference between a constructor and function, because a constructor is essentially a function.

 

however, if you just give user a raw constructor, there is no way for the user to mistakenly call it like a normal function. and the consequence is sometimes horrible.

 

 

e.g 

function User(first, last){
  this.name = first + " " + last;
}
var name = "Resig";
var user = User("John", name);
assert( name == "John Resig", "The name variable is accidentally overridden." );

 

 

How to avoid such kind of thing, here is one deal.

 

/**************************************
*@Summary
* Make sure that the constructor is called in the right way 
*
* Make use of the arguments.callee trick; After this trick, you can find the User function behave like a constructor and a static constructor at the same time.
*
* @Usage:
*   

var name = "Resig";
var user = User1("John", name);
assert( name == "John Resig",
"The name variable is accidentally overridden." );


now, what you can do is to do this :

var name = "Resig";
var user = User("John", name);
assert(user, "This was defined correctly,even if it was by mistake");

* @TODO:
* test it 
***************************************/


function User1(first, second) {
  this.name = first + " " + second;
}


function User(first, second) {

  if (!(this  instanceof arguments.callee)) {
    return new User(first, second);
  }

  this.name = first + " " + second;
}
 

 

 

the statement, 'this instanceof arguments.callee will test if the 'this' is of the type of the callee, in this case, "User", and if it is, just run the constructor as normal (the else part), however, if 'this' is not the 'User', it will instead call and return the constructor.

 

 

 

 

below is the html script that test the function. HTH.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="instantiation.js" type="text/javascript"></script>
    <script src="../unit.js" type="text/javascript"></script>
    <script type="text/javascript">
      window.onload = function () {
        test("test the instantiation of the function", function () {
          var name = "Resig";
          var user = User("John", name);

          assert(user, "This was defined correctly,even if it was by mistake");
        });
      };
    </script>
    <style type="text/css">
      #results li.pass { color: Green }
      #results li.fail { color: Red }
    </style>
</head>
<body>
<ul id="results" />
</body>
</html>
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值