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 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 ...
Listing 1 in this section outlines a basic strategy for handling stream errors, including checking the status and taking appropriate actions based on the error type. #### Setting Up Socket Streams ...
The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................
- **Message Forwarding:** If an object receives a message that does not match any of its methods, the runtime system can forward the message to another object. This process involves a series of steps,...
The `RESET` option for the `ALTER TYPE` statement allows developers to reset an object type to its original state, undoing any changes made since its creation. This feature is useful for testing and ...
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 ...
12. **Type Checking with `instanceof`**: Discusses the use of `instanceof` for checking the type of an object and its limitations. 13. **Browser Incompatibilities**: Highlights differences in behavior...
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 ...
Pointer collection classes can also be configured for arbitrary pointer use, allowing them to hold any type of pointer, including non-object pointers. #### Working with Collections: Copying and ...
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 ...