原型链原型链原型链原型链原型链
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //原型链 : 实例对象与原型之间的连接,叫做原型链
- //原型链的最外层 : Object.prototype
- function Aaa(){
- //this.num = 20;
- }
- //Aaa.prototype.num = 10;
- Object.prototype.num = 30;
- var a1 = new Aaa();
- alert(a1.num);
- </script>
- </head>
- <body>
- </body>
- </html>
hasOwnProperty
是用来判断一个对象是否有你给出名称的属性或对象。不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员。
isPrototypeOf是用来判断要检查其原型链的对象是否存在于指定对象实例中,是则返回true,否则返回false。
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //hasOwnProperty : 看是不是对象自身下面的属性
- var arr = [];
- arr.num = 10;
- Array.prototype.num2 = 20;
- //alert( arr.hasOwnProperty('num') ); //true
- alert( arr.hasOwnProperty('num2') ); //false
- </script>
- </head>
- <body>
- </body>
- </html>
constructor
属性返回对创建此对象的数组函数的引用。
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //constructor : 查看对象的构造函数
- /*function Aaa(){
- }
- var a1 = new Aaa();
- alert( a1.constructor ); //Aaa
- var arr = [];
- alert( arr.constructor == Array ); //true*/
- /*function Aaa(){
- }
- //Aaa.prototype.constructor = Aaa; //每一个函数都会有的,都是自动生成的
- //Aaa.prototype.constructor = Array;
- var a1 = new Aaa();
- alert( a1.hasOwnProperty == Object.prototype.hasOwnProperty ); //true*/
- /*function Aaa(){
- }
- Aaa.prototype.name = '小明';
- Aaa.prototype.age = 20;
- Aaa.prototype = {
- constructor : Aaa,
- name : '小明',
- age : 20
- };
- var a1 = new Aaa();
- alert( a1.constructor );*/
- function Aaa(){
- }
- Aaa.prototype.name = 10;
- Aaa.prototype.constructor = Aaa;
- for( var attr in Aaa.prototype ){
- alert(attr);
- }
- </script>
- </head>
- <body>
- </body>
- </html>