js原型

1.了解原型与原型链之前,首先要弄明白什么是普通对象和函数对象;即任何对象都是由一个构造函数创建的,但是不是每一个对象都有prototype,只有方法才有prototype。

var o1 = {}; 
var o2 =new Object();
var o3 = new f1();

function f1(){}; 
var f2 = function(){};
var f3 = new Function('str','console.log(str)');

console.log(typeof Object); //function 
console.log(typeof Function); //function  

console.log(typeof f1); //function 
console.log(typeof f2); //function 
console.log(typeof f3); //function   

console.log(typeof o1); //object 
console.log(typeof o2); //object 
console.log(typeof o3); //object

在上面的例子中 o1 o2 o3 为普通对象,f1 f2 f3 为函数对象。怎么区分,其实很简单,凡是通过 new Function() 创建的对象都是函数对象,其他的都是普通对象。f1,f2,归根结底都是通过 new Function()的方式进行创建的。Function Object 也都是通过 New Function()创建的

2.什么是构造函数

function Person(name){
    this.name=name || '小薇薇'
	this.age="26"
}
			
Person.prototype.sex = '男'
Person.prototype.eat=function (food){
	console.log(this.name+'今天中午吃的'+food)
}
			
var person = new Person('小花花')
console.log(person.sex)//'男'
person.eat('大米') //'小花花今天中午吃的是大米'

上面的例子中 person 是 Person 的实例。这个实例有一个 constructor (构造函数)属性,该属性(是一个指针)指向 Person。 即:

console.log(person.constructor==Person) //true

3.什么是原型对象呢?

Person.prototype = {
   name:  'Zaxlct',
   age: 28,
   job: 'Software Engineer',
   sayName: function() {
     alert(this.name);
   }
}

原型对象,顾名思义,它就是一个普通对象。从现在开始你要牢牢记住原型对象就是 Person.prototype ,如果你还是害怕它,那就把它想想成一个字母 A: var A = Person.prototype 即:

var A = Person.prototype = {
   name:  'Zaxlct',
   age: 28,
   job: 'Software Engineer',
   sayName: function() {
     alert(this.name);
   }
}

在上面我们给 A 添加了 四个属性:name、age、job、sayName。其实它还有一个默认的属性:constructor.在默认情况下,所有的原型对象都会自动获得一个 constructor(构造函数)属性,这个属性(是一个指针)指向 prototype 属性所在的函数(Person)

即:

Person.prototype.constructor == Person //true 
person.constructor ==Person //true 

结论:原型对象(Person.prototype)是 构造函数(Person)的一个实例。

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

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

Function.prototype 为什么是函数对象呢?

上文提到凡是通过 new Function( ) 产生的对象都是函数对象。因为 A 是函数对象,所以Function.prototype 是函数对象。

那原型对象是用来做什么的呢?主要作用是用于继承。举个例子:

var Person = function(name){
    this.name = name; 
  };
  Person.prototype.getName = function(){
    return this.name;  
  }
  var person = new Person('小伟');
  person.getName(); //小伟

从这个例子可以看出,通过给 Person.prototype 设置了一个函数对象的属性,那有 Person 的实例(person1)出来的普通对象就继承了这个属性。两次 this 在函数执行时都指向 person。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值