- 浏览: 399622 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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>
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 926the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2252Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1657In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 904In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 699Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 971Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2076In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1023We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 817For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 879Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 764There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 764All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 520though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 547when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 515When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 745while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 531IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1040there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1037expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1142it is not as simple as you thin ...
相关推荐
在这个“wvent_instantiation_assignment.rar”压缩包中,我们重点关注的是一些与实现GSM短信功能相关的辅助函数。这些函数通常是为了简化处理短信服务的流程而设计的,它们可以方便地集成到Android应用中,以实现...
3.3 RAM Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.4 RESET Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 Synthesis 6 4.1 Read Design . . . . . . . . . ...
Instantiation.py
1.4 VxWorks Configuration and Build .............................................................................. 5 2 VxWorks Configuration ..............................................................
3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.2 Structure of this document . . . . . . . . . ...
Instantiation Chapter 27. Templates and Hierarchies Chapter 28. Metaprogramming Chapter 29. A Matrix Design Part IV: The Standard Library Chapter 30. Standard Library Summary Chapter 31. STL ...
26. Instantiation ................................................................................ 741 27. Templates and Hierarchies .......................................................... 759 28. ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
去实用程序 Go-utils 是一个用 Go 编写的库,具有一些对其他人有用的功能。 我用不同的语言(Ruby、Java 等)进行了编程,并且总是有一些你想要从特定语言中实现的东西(例如map 、从 Ruby 中select方法、Java 中的...
26. Instantiation ....................................................................741 27. Templates and Hierarchies .......................................................759 28. Metaprogramming ....
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
The multiplier instantiation in the verilog files are used when targeting Virtex 2. For all other devices, the instantiations are commented out and the behavioral multiplier code is used. Test_dct ...
The relationship between MXML and ActionScript classes. . . . . . . . . . . . . . . . . . 37 How MXML relates to standards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43...
asiodrivers.cpp - ASIO driver managment (enumeration and instantiation) ASIOConvertSamples.h ASIOConvertSamples.cpp - sample data format conversion class ginclude.h - platform specific definitions ...
- **Constructors and Destructors:** Discussion on constructors and destructors, including automatic instantiation and cleanup of objects. #### Part III: Using PHP in Practice **Chapter 9: Handling ...
Design 9Factory Method - MotivationFactory Method pattern is useful when you want to defer the instantiation to subclasses. This allows you to define the type of objects to create while letting ...
- **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...
由于Typescript的堆栈限制,某些regEx因为它们太长而停止工作,并触发称为Type instantiation is excessively deep and possibly infinite递归堆栈溢出,其Type instantiation is excessively deep and possibly ...
Build classes that compose one or more traits for full reusability, or create new functionality by mixing them in at instantiation Table of Contents Part I. Core Scala Chapter 1. Getting Started With...