Advanced ASP.NET AJAX Server Controls, For .NET Framework 3.5 (2009)

Advanced ASP.NET AJAX Server Controls, For .NET Framework 3.5 (2009)

(Parte 5 de 5)

After we add a property or a function to an object, we can set its value back to nullby assigning nullto it, as shown here:

myCar.print = null;

This is useful, but if we iterate over this object using the for…inloop, printwill still be included in the loop. If you don’t want the property at all anymore and want to completely remove it, you can delete it from the object using the deletecommand as follows:

delete myCar.print;

Deleting a property removes it from the object so that it will no longer be returned from the for…inloop, and if accessed, it returns undefined.

JavaScript Object Notation (JSON) JSON is a data interchange format similar to XMLin purpose, but lighter than XMLwhen compared on the number of characters needed to define an object with the same content. Listing 1.1 shows the JSON and XMLneeded to create an object with the same content.

Listing 1.11JSON versus XML

"Make":"Ford", "Model":"Explorer", "Year": 2003, "Type": "SUV", "PreviousOwners" : ["Tony","Mark","Susan"] }, {

"Make":"Honda", "Model":"Accord", "Year": 1999, "Type": "Sedan", "PreviousOwners" : ["Stacey","Bailey","Robin"] } ] <Cars>

<Car>

<Make>Ford</Make>

<Model>Explorer</Model>

<Year>2003</Year>

<Type>SUV</Type>

<PreviousOwners>

<Owner>Tony</Owner>

<Owner>Mark</Owner>

<Owner>Susan</Owner>

</PreviousOwners>

</Car>

<Car>

<Make>Honda</Make>

<Model>Accord</Model>

<Year>1999</Year>

<Type>Sedan</Type>

<PreviousOwners>

<Owner>Stacey</Owner>

<Owner>Bailey</Owner>

<Owner>Robin</Owner>

</PreviousOwners>

</Car>

</Cars>

JSON’s syntax is based on the Object Literal notation briefly covered earlier, but the rules for creating valid JSON are stricter in comparison.

The first rule difference is that property names must be enclosed in full quotation marks. In Object Literal notation, they can be enclosed in full quotation marks, single quotation marks, or nothing at all as long as they

Strings 13 don’t conflict with a reserved word. In JSON, every property name must be enclosed in quotes.

The other rule difference is the type of objects that can be assigned to properties. In Object Literal notation any primitive type or object can be assigned to a property. Not so in JSON. In JSON, only primitive data types, arrays, null, and object literals can be assigned to properties. Because it’s used as a data interchange format, it makes sense that functions and other objects aren’t allowed to be assigned to properties.

Chapter 1:Programming with JavaScript14

NOTEJSON in ASP.NETAJAX

JSON is the default response format forweb services that are made callable to client code.

Primitive Data Type Wrapper Objects Each of the primitive data types, boolean, number, and string, has a wrapper object that’s accessed through the capitalized version of the primitive data type: Boolean,Number, and String. The wrapper objects derive from theObjectdata type and contain the methods (substring,length, toString, and so on) that we seemingly use directly off variables that point to primitive data types. Because the primitive data types just store data and don’t have any methods or properties available to them, JavaScript silently converts the primitive data types to and from the wrapper objects so that we can use methods without having to cast to another object first.

As an example of this implicit conversion, consider the lengthproperty that we use on the stringdata type shown in Listing 1.12.

Listing 1.12Implicit Conversion to Primitive Wrapper Object var x = "Hello!"; var len = x.length;

Now when we use the lengthproperty of the xvariable, JavaScript actually converts the primitive string data type to its wrapper Stringobject and then executes the lengthproperty on it.

instanceof instanceofdetermines whether an object derives from a particular type. This operation can prove useful when programming systems that work with inherited and derived types, but it works only with objects and does not work with the primitive types. Listing 1.13 demonstrates instanceofin action.

Listing 1.13 instanceof var x = new Date(); alert (x instanceof Date); // alerts true alert (x instanceof Object); // also returns true.

As you might expect, xis an instance of a Dateand an Objectbecause theDatetype derives from Object.

The constructor Property Theconstructorproperty references the function that created an object. It can be used to determine an object’s type. It is similar to the instanceof operator, but instead of testing to see whether the object is derived or is of a particular type, the constructorproperty returns the name of the function that created the object, which is really the object’s exact type. Listing 1.14 shows how to use the constructorproperty.

Listing 1.14The constructor Property var x = new Number(); if (x.constructor === Number) { alert ("x is a number!"); }

This code snippet will alert “x is a number!” because we used the

Numberconstructor method to create our object.

Theconstructorproperty is key to mimicking a classic object-oriented system, and we cover how to use it for this purpose in the “Object-Oriented JavaScript Programming” section.

Strings 15

Variables and Function Arguments

Equality JavaScript provides two differenttypes of equality models: strict and not strict. The strict equality operators compare both the value and type of the operands and are accessed through the operators ===and!==, which are often referred to as strict equal and strict not equal. The not strict equality operators, accessed through the operators ==and!=, compare the operands based solely on value. If the operands are not the same type, such as a numberand a string, JavaScript attempts to perform a type conversion to an appropriate type before performing the comparison. Both equality models are useful depending on what you’re trying to accomplish, but the strict equality operators perform a bit better because they don’t require any type conversions before they’re executed. Most of the JavaScript code that we’l write and that Microsoft wrote in the Microsoft AJAX Library uses the strict equality operators when a comparison is required.

Scope Variables in JavaScript arescoped differently than in other block-scoped languages such as C# or Java. In JavaScript, only the global object (the windowobject in browsers) and functions are variable scope boundaries. Other blocks, such as if-then-elsestatements and whileloops, don’t provide a variable scope boundary. This is in contrast to other languages such as C#, where scope can be created anywhere by enclosing code within a pair of curly braces ({}).

Chapter 1:Programming with JavaScript16

TIPconstructor Property of Primitive Data Types

Because the primitive data types don’t inherit from Object, you might be wondering what’s returned when we examine their constructor properties. Well, at least in Internet Explorer 7 and Firefox, the wrapper object’s constructor that’s associated to each primitive data type is returned. It seems that even when attempting to access the constructorproperty, JavaScript casts the primitive types to their wrapper objects.

(Parte 5 de 5)

Comentários