Home >Web Front-end >JS Tutorial >A brief discussion on Object and Function objects in Javascript_javascript skills

A brief discussion on Object and Function objects in Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:37:591453browse

1.Object

Prototype object
The prototype is an attribute of the object, that is, the prototype attribute. Every object has this internal attribute, and it is also an object itself.

<script type="text/javascript">
   Object.prototype.num= 10;
   alert("添加原型对象属性:"+ Object.num);
   Object.num = 20;
   alert("添加对象属性:"+Object.num);
</script>

Prototype Chain

Object.prototype.a = 3.14;
alert("Instance of Object object:" new Object().a);
alert("Properties of String object:" String.a);


Analysis: When the prototype of Object is extended, it is equivalent to the object becoming Object.prototype, that is, all local objects have the properties of this object. Because all local objects inherit the Object object, String also has the value of attribute a.

2.Function object

arguments object
When a function is executed, the system will automatically create an arguments object attribute for the function object. The arguments object attribute can only be used in the function body and is used to manage the actual parameters of the function.

(1) caller attribute

The caller attribute shows the caller of the function, so in the following example, the caller of function a is function b(); the caller of function b is null;

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

(2) length attribute

                                                                                                                                                                                                                                    Length is a property of the arguments object, which indicates the number of parameters passed when the function is called. An actual parameter can be accessed through an array.

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

The running result is 6

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn