JS面向?qū)ο?/h2>
1. 創(chuàng)建對(duì)象
- 字面量創(chuàng)建
var people={
name:"iwen",
age:30,
eat:function(){
alert("能吃");
}
}
people.eat();
- new/原型鏈創(chuàng)建
function People(){
}
People.prototype={
name:"iwen",
age:30,
eat:function(){
alert("能吃");
}
}
var p=new People();
p.eat();
- return
function Person(name)={
var _this={}
_this._name=name;
_this.eat=function(){
alert(_this.name+"能吃");
}
return _this;
}
- Object.create
var obj=Object.create({x:1});
obj.x;//1
2. 對(duì)象繼承
function People(){
}
People.prototype.eat=function(){
alert("能吃");
}
function Student(){
}
Student.prototype=new People();
var stu=new Student();
stu.eat();
///
function Person(name){
var _this={}
_this._name=name;
}
return _this;
}
function Student(name){
var _this=Person;
return _this;
}
//
function Person(){
}
function Student(){
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor=Student;
var people={
name:"iwen",
age:30,
eat:function(){
alert("能吃");
}
}
people.eat();
function People(){
}
People.prototype={
name:"iwen",
age:30,
eat:function(){
alert("能吃");
}
}
var p=new People();
p.eat();
function Person(name)={
var _this={}
_this._name=name;
_this.eat=function(){
alert(_this.name+"能吃");
}
return _this;
}
var obj=Object.create({x:1});
obj.x;//1
function People(){
}
People.prototype.eat=function(){
alert("能吃");
}
function Student(){
}
Student.prototype=new People();
var stu=new Student();
stu.eat();
///
function Person(name){
var _this={}
_this._name=name;
}
return _this;
}
function Student(name){
var _this=Person;
return _this;
}
//
function Person(){
}
function Student(){
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor=Student;
如果子類也有一個(gè)eat()Student.prototype.eat=function(){alert("學(xué)生能吃")}
則只調(diào)用子類的方法;可以通過(guò)var SuperSsay=Student.prototype.say
,然后在子類方法中加上SuperSsay.call(this)
來(lái)調(diào)用父類的同名方法。
3. 對(duì)象封裝
(function(){
function Student(name){
this._name=name;
}
Student.prototype=new People();
var stu=new Student();
window.Student=Student;
}())
4.原型鏈
function foo(){
foo.prototype.z=3;
}
var obj=new foo();
obj.x=1;
obj.y=2;
obj.x;//1對(duì)象屬性有
obj.y;//2對(duì)象屬性有
obj.z;//3對(duì)象屬性沒(méi)有捻艳,則查找對(duì)象的原型鏈
obj.hasOwnPrototype('z');//false 因?yàn)樵蛯傩詻](méi)有'z'
obj.z=5;//因?yàn)樵蜎](méi)有屬性z丑念,所以會(huì)在原型上新建一個(gè)屬性z=5,對(duì)原型鏈上的屬性z沒(méi)影響
delete obj.z;//true 并刪除原型上的屬性z浓利,不能刪除原型鏈上的屬性z
obj.z;//3
//如果使用Object.create方法創(chuàng)建對(duì)象
var obj=Object.create({x:1});
obj.x;//1
obj.hasOwnPrototype('x');//false,是因?yàn)閛bj的原型鏈指向{x:1}這個(gè)對(duì)象,x是原型鏈中的屬性,obj的原型里面并沒(méi)有這個(gè)屬性
對(duì)象有__proto__屬性,函數(shù)有prototype屬性珍逸,函數(shù)也是一個(gè)對(duì)象所以函數(shù)也有__proto__屬性,而對(duì)象的__proto__屬性指向構(gòu)造這個(gè)對(duì)象的函數(shù)的函數(shù)原型聋溜,即
function obj(){}
var o=new obj();
console.log(o.__proto__===obj.prototype);//true
函數(shù)的__proto__屬性指向構(gòu)造這個(gè)函數(shù)的函數(shù)的原型谆膳,比如函數(shù)是像function a(){}
這樣定義的則a.__proto__===Function.prototype
,如果函數(shù)是像var a={}
,或者var a=new Object()
等方法定義的撮躁,則a.__proto__===Object.prototype
摹量。
函數(shù)的原型即prototype屬性包含constructor屬性,指向函數(shù)本身,即obj.prototype.constructor===obj
缨称。瀏覽器中還包含一個(gè)__proto__屬性(Object.prototype.__proto__)
但不推薦使用