Apress ProJavaScriptTechniques
The first way of checking the type of an object is by using the obvious-sounding typeof operator.
js 代码
-
- if ( typeof num == "string" )
-
- num = parseInt( num );
-
- if ( typeof arr == "string" )
-
- arr = arr.split(",");
The second way of checking the type of an object is by referencing a property of all JavaScript objects called constructor.
js 代码
-
- if ( num.constructor == String )
-
- num = parseInt( num );
-
- if ( str.constructor == Array )
-
- str = str.join(',');
Table 2-1 shows the results of type-checking different object types using the two different methods that I’ve described.
Variable<o:p></o:p>
|
typeof Variable<o:p></o:p>
|
Variable.constructor<o:p></o:p>
|
{ an: “object” }<o:p></o:p>
|
object<o:p></o:p>
|
Object<o:p></o:p>
|
[ “an”, “array” ]<o:p></o:p>
|
array<o:p></o:p>
|
Array<o:p></o:p>
|
function(){}<o:p></o:p>
|
function<o:p></o:p>
|
Function<o:p></o:p>
|
“a string”<o:p></o:p>
|
string<o:p></o:p>
|
String<o:p></o:p>
|
55<o:p></o:p>
|
number<o:p></o:p>
|
Number<o:p></o:p>
|
true<o:p></o:p>
|
boolean<o:p></o:p>
|
Boolean<o:p></o:p>
|
new User()<o:p></o:p>
|
object<o:p></o:p>
|
User<o:p></o:p>
|
Strict typechecking can help in instances where you want to make sure that exactly the right number of arguments of exactly the right type are being passed into your functions.
js 代码
-
- function strict( types, args ) {
-
- if ( types.length != args.length ) {
- throw "Invalid number of arguments. Expected " + types.length +
- ", received " + args.length + " instead.";
- }
-
- for ( var i = 0; i < args.length; i++ ) {
-
- if ( args[i].constructor != types[i] ) {
- throw "Invalid argument type. Expected " + types[i].name +
- ", received " + args[i].constructor.name + " instead.";
- }
- }
- }
-
- function userList( prefix, num, users ) {
-
-
- strict( [ String, Number, Array ], arguments );
-
- for ( var i = 0; i < num; i++ ) {
-
- print( prefix + ": " + users[i] );
- }
- }
分享到:
相关推荐
the JavaMail API, if you want this type of information, you have to calculate it yourself. IMAP IMAP is a more advanced protocol for receiving messages. Defined in RFC 2060 , IMAP stands for ...
type checking to catch our mistakes earlier on. In chapter nine we arrange for automatic initialization to prevent another class of bugs. Chapter ten introduces delegates and shows how classes and ...
- FIX: In "Windows ClearType" font rendering mode (OS Windows mode) the "garbage" pixels can appear from the right and from the bottom sides of the painted rectangle of the TFlexText object....
The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................
The definition of an inline function needs to be in a header file, so that the compiler has the definition available for inlining at the call sites. However, implementation code properly belongs in ....
In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...
Managing categories the easy way 243 Defining JParameters using XML 245 Creating a JParameter object 247 Rendering a JParameter object 248 Saving JParameter data 251 Getting and setting values in...
2.6 Sorting a List of Objects by an Attribute of the Objects 2.7 Sorting by Item or by Attribute 2.8 Selecting Random Elements from a List Without Repetition 2.9 Performing Frequent Membership ...
If you currently work with VCLZip 2.X with TBlobStreams or some other type of streams, you can either define your own TkpBlobStream for instance which inherits from TkpHugeStream, or use the ...
Sorting a List of Objects by an Attribute of the Objects Recipe 5.4. Sorting Keys or Indices Basedon the Corresponding Values Recipe 5.5. Sorting Strings with Embedded Numbers Recipe 5.6. ...
1.2.3 Targets of the language....................................................................................5 1.2.4 Guiding design principles........................................................
Fixed a bug when sending email to more than two address (the separator is still ‘,‘).9/3/981.6.0- Sometimes the filenames of an attachment contain invalid chars making very dificult to open a ...
imperative, and object-oriented styles in the same program and exploit the strengths of each paradigm. Like other typed functional languages, F# is strongly typed but also uses inferred typing so ...
You should always check for the type and version of the client browser, so that your content degrades gracefully if the client browser does not support features on your Web site. The easiest way to ...
I also tried an assorted number of sorting routines like double pivot quick sort, timsort, insertion sort and found that they all were slower than the internal .net quicksort routine in my tests. ...
1 , WinLocaleConvert.zip This program shows the international settings of the country you select such as Format Currency, Date Format, Day Name, Month Name...<END><br>2 , netstuff.zip This ...
With these routines, one can address the filter as a virtual object, with little need for detailed understanding of its behavior. This simplifies the programming task for those who want to integrate ...
This book will teach you how to develop JavaScript applications with simple to use, yet powerful JavaScript technologies and host everything in the cloud in an economic and robust way in AWS. Enjoy an...
The latest versions of ICS can be downloaded from the ICS Wiki web site: http://wiki.overbyte.be/wiki/index.php/ICS_Download ICS V5 and V6 are archive releases no longer updated, last supported ...
Tricks of the Windows video Game Programming <br>PART I Windows Programming Foundations 7 1 Journey into the Abyss 9 A Little History.............................................................