Javascript object related operators

Short about operators with objects in Javascript.

Using the "new" operator

  • To create new objects in Javascript, we use the "new" operator, which you probably have seen already.
  • New objects can be created either on the basis of one of the built-in objects such as Number, String, Object, Function, etc., or based on a function, which is also an object.

    Examples:
    <script type="text/javascript">
      // create number object
      var num = new Number(45);
      document.write("Number: "+num+"<br>");
    
      // create string object
      var str = new String("This is a string");
      document.write("String: "+str+"<br>");
    
      // create function object
      var mult = new Function("op1","op2"," return op1*op2;");
      // A more common way to create function objects:
      var multiply = function(op1,op2) {return op1*op2;};
      document.write("multiply(2,3): "+multiply(2,3)+"<br>");
    
     // create an object using a function object
      var multiply = function(op1,op2) {
        this.toString=function(){ return op1*op2;};
      };
      var obj=new multiply(2,4);
      document.write("As object: "+obj+"<br>");
    </script>

Using the "delete" operator

  • With the "delete" operator, you can delete the object properties, array element, or variable specified as its operand.
  • Variable defined in a function object with the 'var' keyword can not be removed.
  • Some properties, found in the embedded objects are immune and can not be removed.
  • "delete" operator returns true if it succeeds in deleting, otherwise the return value is false.
  • "delete" operator also returns true if the operand does not exist or operand is not a property, array element, or variable.

    Examples:
    <script type="text/javascript">
      // create an object
      var obj={height : 10, width : 20,
        area : function(){return this.height *this.width;}
      };
      document.write("area : "+obj.area()+"<br>");
      // delete a property in the object
      var status=delete(obj.width);
      document.write("delete return : "+status+"<br>");
      document.write("area : "+obj.area()+"<br>");
    
      // create an array
      var arr=[1,,1,121,17,118,19];
      document.write("arr[0] arr[3] arr[5] : "+arr[0]+" "+arr[3]+" "+arr[5]+"<br>");
      // delete the arr[3]
      status=delete(arr[3]);
      document.write("delete return : "+status+"<br>");
      document.write("arr[0] arr[3] arr[5] : "+arr[0]+" "+arr[3]+" "+arr[5]+"<br>");
    
      // delete a global variable
      test=10;
      document.write("test: "+test+"<br>");
      status=delete(test);
      document.write("delete return : "+status+"<br>");
      try {
        document.write("test: "+test+"<br>");
      }catch(e){
        document.write("ERROR: "+e+"<br>");
      }
    </script>

Using the "in" operator

  • The "in" operator can be used to test for the existence of a property in an object.
  • The name of the property is set as a string in front of the operator, which is followed by the object to be tested.

    Examples:
    <script type="text/javascript">
      var person={name : 10, age : undefined  };
      document.write("'name' in 'person' returns : "+ ("name" in person) +"<br>");
      // age is undefined, but exists
      document.write("'age' in 'person' returns : "+ ("age" in person) +"<br>");
    </script>

Using the "instanceof" operator

  • The "instanceof" operator expects a left-side operand that is an object and a right-side operand that is the name of an object with a constructor function.
  • Object with a constructor function may be a function object or built-in Javascript type, such as date, Number, String, Object, etc.
  • Since objects can inherit from other objects, an object will be an instance of all the objects in the inheritance chain. This means also that all objects is an instance of the Object object.
  • The operator evaluates to true if the left-side object is an instance of the right-side object (with a constructor function) and evaluates to false otherwise.

    Examples:
    <script type="text/javascript">
      var mon = ["January","June","May"];   // Create an array
      document.write("mon instanceof Array evaluates to: "
        + (mon instanceof Array) +"<br>");
      document.write("mon instanceof Object evaluates to: "
        + (mon instanceof Object) +"<br>");
      var Shape= function(point) {         // create a function
        this.point=point;
      }
      var sha= new Shape([34,45]);
      document.write("sha instanceof Shape evaluates to: "
        + (sha instanceof Shape) +"<br>");
      document.write("sha instanceof Function evaluates to: "
        + (sha instanceof Function) +"<br>"); // false
      document.write("sha instanceof Object evaluates to: "
        + (sha instanceof Object) +"<br>");
    
      document.write("Shape instanceof Function evaluates to: "
        + (Shape instanceof Function) +"<br>"); // false
    </script>

Using the "typeof" operator

  • If you want to know the data type of a variable or a value, you can use the special typeof operator.
  • This operator returns a string that represents the data type. The return values of using typeof can be one of the following—"number", "string", "boolean", "undefined", "object", or "function".
  • Remember that typeof "null is "object", while typeof "undefined is "undefined".
  • The type of any array is "object" because all arrays are objects, but type of any function is "function", even though functions are objects, too
  • The typeof operator is placed in front of its single operand, which can be of any type.

    Examples:
    <script type="text/javascript">
      var mon = ["January","June","May"];   // Create an array
      document.write("type of array: " + (typeof mon) +"<br>");
      var date = new Date()   // Create a date
      document.write("type of date: " + (typeof date) +"<br>");
      var num = 324;   // Create an array
      document.write("type of number: " + (typeof num) +"<br>");
      var str = "324";   // Create an array
      document.write("type of string: " + (typeof str) +"<br>");
      var func = function() {};
      document.write("type of function: " + (typeof func) +"<br>");
      var myRef = null;
      document.write("type of null: " + (typeof myRef) +"<br>");
      var myund = undefined;
      document.write("type of undefined: " + (typeof myund) +"<br>");
      var x;
      document.write("type of x: " + (typeof x) +"<br>");
    </script>

Special use of the "||" operator

  • Although the || operator is usually used only as a boolean OR operator between two boolean values ​​(an operand on its left side and an operand on its right side), it can also be used for other value types that basically is not boolean.
  • Any expression or operand that evaluates to null, NaN, 0, "" or undefined are considered to be a false boolean value. All other results of such evaluation is considered to be a true boolean value
  • The actual behavior of this operator is that it starts by evaluating its first operand, the expression on the left side. If the value of this expression evaluates to true, the operator returns the value of the left-side expression. Otherwise, it evaluates its second operand, the expression on the right, and returns the value of that expression.

    Example:
    <script type="text/javascript">
      var eval = function(obj) {
        var ret=obj || [3,4,5];
        return ret;
      }
      var mon = ["January","June","May"];   // Create an array
      document.write("function return with an argument : " + eval(mon) +"<br>");
      document.write("function return with NO argument : " + eval() +"<br>");
    </script>

Special use of the "&&" operator

  • Although the && operator is usually used only as a boolean AND operator between two boolean values ​​(an operand on its left side and an operand on its right side), it can also be used for other value types that basically is not boolean.
  • Any expression or operand that evaluates to null, NaN, 0, "" or undefined are considered to be a false boolean value. All other results of such evaluation is considered to be a true boolean value
  • The actual behavior of this operator is that it starts by evaluating its first operand, the expression on the left side. If the value of this expression evaluates to false, the operator returns the value of the left-side expression. Otherwise, it evaluates its second operand, the expression on the right, and returns the value of that expression.

    Example:
    <script type="text/javascript">
      var eval = function(obj) {
         var ret=obj && [3,4,5] ;
        return ret;
      }
      var mon = ["January","June","May"];   // Create an array
      document.write("function return with an argument : " + eval(mon) +"<br>");
      document.write("function return with NO argument : " + eval() +"<br>");
    </script>

Prev. in menu javascript/Operators/ObjectOperators.php Next in menu
Prev. in menu javascript/Operators/ObjectOperators.php Next in menu
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.