Tuesday, October 4, 2011

JavaScript II - Expressions and Operators

About Arrays, Objects and Functions

Object Creation Expressions

new Object()
new Point(2,3)
If no arguments are passed to the constructor function in an object creation expression, the empty pair of parentheses can be omitted:
new Object
new Date

Object and Array Initializers

Object and array initializers are expressions whose value is a newly created object or array.

var arrayOfArrays = [[1,2], [3]];
var obj = { x:1, y:’otv’}; // Object with 2 properties

Property Access Expressions

Two syntaxes for property access:

var obj = {x:5,y:2};
obj.x;    // 5 expression.identifier
obj['y']; // 2 expression [ expression ]

Function Definition Expressions

var duplicate = function(x){return x*2;}; // Function definition
duplicate(2);

Operators

Operator Description Input Output
void Return undefined value any undef
*, /, % Multiply, divide, remainder num,num num
delete

Unary operator that attempts to delete the object property or array element specified as its operand

// Deleting object properties
var obj = {x:1,y:2};
obj.x;  //Before deleting property value=1
delete obj.x;
obj.x; //After deleting property value:undefined
// Deleting array elements
var array = [10,20];
array.length;   // 2
array[0];       // 10
delete array[0];// true
array.length;   // 2
array[0];       // undefined

Not all properties can be deleted, however: some built-in core and client-side properties are immune from deletion, and user-defined variables declared with the var statement cannot be deleted. Functions defined with the function statement and declared function parameters cannot be deleted either.


property or array element

bool
typeof

Determine type of operand.

var trueTypeOf = typeof true; // “boolean”

The following table specifies the value of the typeof operator for any JavaScript value:

Parameter Return
undefined "undefined"
null "object"
true or false "boolean"
any number or NaN "number"
any string "string"
any function "function"
any nonfunction native object "object"
any host object An implementation-defined string, but not “undefined”, “boolean”, “number”, or “string”.

any str
in

Test whether property exists.It evaluates to true if the left-side value is the name of a property of the right-side object. For example:

var person = { name:’Bob’, address:’Marylan’}; // Define an object
"name" in person    // true: object has property named "name"
"toString" in person// true: object inherits toString method
var data = [10,20]; // An array with elements 0 and 1
"0" in data         // true: array has an element "0"
5 in data           // false: no element 5
str,obj bool
instanceof

Test object class. It expects a left-side operand that is an object and a right-side operand that identifies a class of objects. The operator evaluates to true if the left-side object is an instance of the right-side class and evaluates to false otherwise. Take into account that in JavaScript, classes of objects are defined by the constructor function that initializes them. Thus, the right-side operand of instanceof should be a function.Examples:

var currentDate = new Date();  // Create a new object with the Date() constructor
currentDate instanceof Date;   // true currentDate was created with Date()
currentDate instanceof Object; // true all objects are instances of Object
currentDate instanceof Number; // false currentDate is not a Number object
obj,func bool
==

Test for equality using a more relaxed definition of sameness that allows type conversions.

any,any bool
!=

Test for inequality

any,any bool
===

Test for strict equality. Known as the strict equality operator (or sometimes the identity operator), and it checks whether its two operands are “identical” using a strict definition of sameness (does not perform type conversion)

any,any bool
!==

Test for strict inequality

any,any bool
?

Ternary operator. Example:

var name = 10>5?'Ten':'Five'; // ‘Ten’
eval

It expects one argument. If you pass any value other than a string, it simply returns that value. If you pass a string, it attempts to parse the string as JavaScript code, throwing a SyntaxError if it fails. If it successfully parses the string, then it evaluates the code and returns the value of the last expression or statement in the string or undefined if the last expression or statement had no value. If the string throws an exception, the eval() propagates that expression.

eval("x=100") //change local variable
eval("function localFunction() { return x*2; }"); //define local function

0 comments:

Post a Comment