js 的prototype 属性和用法,外加__proto__ JavaScript中__proto__与prototype的关系

var ob = { };//超级简单的空对象
alert(JSON.stringify(ob.prototype));
// undefined

能够引用prototype的东西绝对是函数,绝对是函数,绝对是函数,prototype是属于函数的一个属性,prototype是属于函数的一个属性,prototype是属于函数的一个属性,能够引用它的只有·····函数····,能够引用它的只有·····函数·····,能够引用它的只有····函数····,函数,函数,函数,

prototype的应用

1.0 

给原型对象增加函数,就是让对象拥有公用的函数。

例子:我给数组原型对象增加一个打乱数组的方法: //给数组原型增加一个打乱数组的函数

Array.prototype.shuffle = function(){
console.log(this)
let value = this.valueOf(),len = this.length,temp,key;
while(len--){
//随机生成数组的下标
key = Math.floor(Math.random()*len);
//temp为中间交换变量
temp = value[key];
value[key]=value[len]
value[len]=temp;
}
return value
}
let aa = ['1','2','3','4','5'];
console.log(aa.shuffle());
2.0
给原型对象增加属性,也就是给对象增加公用的属性
function fun(){

}  
fun。prototype.name = "小宋";
fun.prototype.arr = ['1','2'];
let a = new fun();
console.log(a.name)
3.0 最主要的属性实现原型继承
  
function P1(){

}
function P2(){

}
//原型对象增加属性和方法
P1.prototype.name="郑1"
P1.prototype.get = function(value){
return value
}
//实例化P2构造函数的一个对象
var o1 = new P1();//这个对象应该包含所有原型对象的属性和方法
console.log(o1);
//给P2的原型对象赋值一个对象,相当于P1继承了o1的所有属性和方法
P2.prototype = o1;//这个式子,简单来讲就类似于a = b, b赋值给a这个总该明白吧?
    //调用P2从o1继承过来的get函数
console.log(P2.prototype.get("123"));
//展示P2从o1继承过来的name属性
console.log(P2.prototype.name);
//用构造函数P2实例化一个o2对象
var o2 = new P2();
//P2的原型对象prototype既然已经继承了o1的所有属性和函数,那么依据P2所实例化出来的对象也都有o1的属性和函数了
console.log(o2)
console.log(o2.name)
console.log(o2.get('456789'))
Array.prototype是一个数组
String.prototype是一个字符串
Object.prototype是一个对象


接下里有个疑问点是__proto__和arguments对象
prototype 是函数的一个属性(每个函数都有一个prototype属性),这个属性时一个指针,
指针,指向一个对象,它是显示修改对象的原型的属性。
__proto__是一个对象的内置属性(请注意:prototype是函数的内置属性,__proto__是对象的内置属性)
是js内部使用寻找原型链的属性。
使用chrome和ff都可以访问到__proto__属性,ie不可以。
new 一个函数的过程得
var Person = function(){}
var p = new Person()
1.0 var p = {}
2.0 p.__proto__ = Person.prototype
3.0 Personal.call(p);也就是构造p,也可以称之为初始化p
4.0 return p
我们console.log(p.__proto__===Person.prototype)
//返回的是true
我们再来一个demo
var person = function(){};
persona.prototype.sayName = function(){
  console.log("my name is body")
}
person.prototype.age = 27;
var p = new person();
p.sayName();
p是一个引用指向person对象,我们在persona的原型商店一了一个sayName
方法和age属性,当我们执行p.age时,会先在this的内部查找(也就是构造函数内部)
,如果没有知道然后再沿着原型链向上追溯,这里的向上追溯是怎么向上的呢?这里就要使用
__proto__属性来链接到原型(也就是Personal.prototype)进行查找.最终在原型上找到了age。
js中_proto_和 prototype的区别和联系、
首先要明确两点
1.0 在js里,也是万物皆对象,方法(Function)是对象,方法的原型(Function.prototype)
是对象,因此,他们都会具有对象共有的特点。
既:对象具有属性__proto__,可称为隐式原型,一个对象的隐式原型指向构造函数的原型这
这保证了实例能够访问在构造函数原型中定义的属性和方法
2.0,方法(Function)
方法这个特殊的对象,除了和其他对象一样有上述__proto__属性之外,还有自己的特有属性
--原型属性(prototype),这个属性是一个指针,指向一个对象,这个对象的用途就是包含
所有实例共享的属性和方法(我们把这个对象叫原型对象),原型对象也有一个属性,叫做
constructor,这个属性包含了一个指针,指回原构造函数。
function Foo(){}
var f1 = new Foo();
var f2 = new Foo();
1.0 构造函数Foo()
构造函数的原型属性Foo.prototype指向了原型对象,在原型对象里有共有的
方法,所有构造函数声明的实例(f1,f2)都可以共享这个方法。
2.0 原型对象Foo.prototype
Foo.prototype保存着实例共享的方法,有一个指针constructor指回构造函数。
3.0 实例
f1和f2是Foo这个对象的两个实例,这两个对象也有属性__proto__,指
向构造函数的原型对象,这样子就可以像上面1所说的访问原型对象的所有
方法和属性了。
另外:
构造函数Foo()出来是方法,也是对象,他也有__proto__属性,指向谁呢?
指向它的构造函数的原型对象,函数的构造函数时function,因此这里的__proto__指向Function.prototype
其实出来Foo(),Function(),Object()也是一样的道理
原型对象也是对象啊,它的__proto__属性,又指向谁呢?
同理,指向它的构造函数的原型对象,这里是Object.prototype
最后,Object.prototype的__proto__属性指向null。
总结:
1.0 对象有属性__proto__,指向该对象的构造函数的原型对象。
2.0 方法除了有属性__proto__,还有属性prototype,prototype指向该方法的原型对象。

JavaScript中__proto__与prototype的关系

这里讨论下对象的内部原型(__proto__)和构造器的原型(prototype)的关系。

所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)

 
1
2
3
4
5
6
7
8
9
Number.__proto__ === Function.prototype   // true
Boolean.__proto__ === Function.prototype  // true
String.__proto__ === Function.prototype   // true
Object.__proto__ === Function.prototype   // true
Function.__proto__ === Function.prototype  // true
Array.__proto__ === Function.prototype    // true
RegExp.__proto__ === Function.prototype   // true
Error.__proto__ === Function.prototype    // true
Date.__proto__ === Function.prototype     // true
 

JavaScript中有内置(build-in)构造器/对象共计12个(ES5中新加了JSON),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎创建,Math,JSON是以对象形式存在的,无需new。它们的__proto__是Object.prototype。如下

 
1
2
Math.__proto__ === Object.prototype   // true
JSON.__proto__ === Object.prototype   // true
这个内置的,我看了有人说9个有人说12个,我认为是9个

这说明什么呢?

 

所有的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身。所有构造器都继承了Function.prototype的属性及方法。如length、call、apply、bind(ES5)。

Function.prototype也是唯一一个typeof XXX.prototype为 “function”的prototype。其它的构造器的prototype都是一个对象。如下

1
2
3
4
5
6
7
8
9
10
console.log( typeof  Function.prototype)  // function
console.log( typeof  Object.prototype)    // object
console.log( typeof  Number.prototype)    // object
console.log( typeof  Boolean.prototype)   // object
console.log( typeof  String.prototype)    // object
console.log( typeof  Array.prototype)     // object
console.log( typeof  RegExp.prototype)    // object
console.log( typeof  Error.prototype)     // object
console.log( typeof  Date.prototype)      // object
console.log( typeof  Object.prototype)    // object

  

噢,上面还提到它是一个空的函数,alert(Function.prototype) 下看看。

知道了所有构造器(含内置及自定义)的__proto__都是Function.prototype,那Function.prototype的__proto__是谁呢?

相信都听说过JavaScript中函数也是一等公民,那从哪能体现呢?如下

1
console.log(Function.prototype.__proto__ === Object.prototype)  // true

这说明所有的构造器也都是一个普通JS对象,可以给构造器添加/删除属性等。同时它也继承了Object.prototype上的所有方法:toString、valueOf、hasOwnProperty等。

最后Object.prototype的__proto__是谁?

1
Object.prototype.__proto__ ===  null   // true

已经到顶了,为null。

二、所有对象的__proto__都指向其构造器的prototype

上面测试了所有内置构造器及自定义构造器的__proto__,下面再看看所有这些构造器的实例对象的__proto__指向谁?

先看看JavaScript引擎内置构造器

1
2
3
4
5
6
7
8
9
10
11
var  obj = {name:  'jack' }
var  arr = [1,2,3]
var  reg = /hello/g
var  date =  new  Date
var  err =  new  Error( 'exception' )
 
console.log(obj.__proto__ === Object.prototype)  // true
console.log(arr.__proto__ === Array.prototype)   // true
console.log(reg.__proto__ === RegExp.prototype)  // true
console.log(date.__proto__ === Date.prototype)   // true
console.log(err.__proto__ === Error.prototype)   // true

再看看自定义的构造器,这里定义了一个Person

1
2
3
4
5
function  Person(name) {
     this .name = name
}
var  p =  new  Person( 'jack' )
console.log(p.__proto__ === Person.prototype)  // true

p是Person的实例对象,p的内部原型总是指向其构造器Person的prototype。

每个对象都有一个constructor属性,可以获取它的构造器,因此以下打印结果也是恒等的

1
2
3
4
5
function  Person(name) {
     this .name = name
}
var  p =  new  Person( 'jack' )
console.log(p.__proto__ === p.constructor.prototype)  // true

上面的Person没有给其原型添加属性或方法,这里给其原型添加一个getName方法

1
2
3
4
5
6
7
8
function  Person(name) {
     this .name = name
}
// 修改原型
Person.prototype.getName =  function () {}
var  p =  new  Person( 'jack' )
console.log(p.__proto__ === Person.prototype)  // true
console.log(p.__proto__ === p.constructor.prototype)  // true

可以看到p.__proto__与Person.prototype,p.constructor.prototype都是恒等的,即都指向同一个对象。

这个是个重点啊!

如果换一种方式设置原型,结果就有些不同了

1
2
3
4
5
6
7
8
9
10
function  Person(name) {
     this .name = name
}
// 重写原型
Person.prototype = {
     getName:  function () {}
}
var  p =  new  Person( 'jack' )
console.log(p.__proto__ === Person.prototype)  // true
console.log(p.__proto__ === p.constructor.prototype)  // false

这里直接重写了Person.prototype(注意:上一个示例是修改原型)。输出结果可以看出p.__proto__仍然指向的是Person.prototype,而不是p.constructor.prototype。

这也很好理解,给Person.prototype赋值的是一个对象直接量{getName: function(){}},使用对象直接量方式定义的对象其构造器(constructor)指向的是根构造器Object,Object.prototype是一个空对象{},{}自然与{getName: function(){}}不等。如下

1
2
3
4
var  p = {}
console.log(Object.prototype)  // 为一个空的对象{}
console.log(p.constructor === Object)  // 对象直接量方式定义的对象其constructor为Object
console.log(p.constructor.prototype === Object.prototype)  // 为true,不解释 

上面代码中用到的__proto__目前在IE6/7/8/9中都不支持。IE9中可以使用Object.getPrototypeOf(ES5)获取对象的内部原型。

1
2
3
var  p = {}
var  __proto__ = Object.getPrototypeOf(p)
console.log(__proto__ === Object.prototype)  // true
 

对象字面量表示法是首选的构造函数

 

JavaScript语言九种内建的构造器:Object()Array()String()Number()Boolean()Date()Function()Error() 以及 RegExp()。当我们需要创建这些值的时候,我们可以自由选择使用字面量或者构造器。但是相同情况下,字面量对象不仅易读,而且运行速度更快,因为他们可以在解析的时候被优化。所以当你需要使用简单对象的时候就使用字面量吧。














































































猜你喜欢

转载自www.cnblogs.com/lieaiwen/p/10210441.html