Home>

JavaScript's object orientation is based on prototypes,All objects have their own prototype chain.Object and function may be a lot of object instanceof function, function instanceof object is true and confused, so first look at the object instance

1.object object

Prototype object

A prototype is a property of an object,That is, the prototype property, each object has this internal property,And he himself is an object.

<script type="text/javascript">
   object.prototype.num=10;
   alert ("Add prototype object properties:" + object.num);
   object.num=20;
   alert ("Add object attribute:" + object.num);
</script>

Prototype chain

object.prototype.a=3.14;

alert ("Instance of object object:" + new object (). a);

alert ("Attribute of string object:" + string.a);

Analysis:When the prototype of the object is extended,Equivalent to the object becomes object. Prototype, that is, all local objects have properties of this object,Because all local objects inherit from the object object, string also has the value of attribute a.

2.function object

arguments object

When a function executes,The system will automatically create a arguments object property for the function object.arguments object properties,Can only be used in function bodies,And used to manage the actual parameters of the function.

(1) caller attributes

The caller property shows the caller of the function,So the following example,Function b is called for function a;null for function b is called;

<script type="text/javascript">
  var a=new function ("alert (" a:"+ a.caller)");
  function b ()
  {
   a ();
   alert ("b:" + b.caller);
  }
  b ();
</script>

(2) the length attribute

length is a property of the arguments object,Represents the number of parameters passed when the function is called,You can access an actual parameter as an array.

function argc ()
{
  alert (arguments [0] + arguments [1] + arguments [3]);
}
argc (1,2,3);

The running result is 6

  • Previous Solve the problem of OutOfMemoryError in Java
  • Next Example of Android phone battery power usage