基於js原型鏈的乙個題

2021-10-02 04:56:50 字數 2273 閱讀 9477

//student是建構函式

function

student

(uname,age,job,hobby)

,我$歲,職業是$,愛好是$`);

// }

// // return 121;

// return;

}student.prototype.

intr

=function()

,我$歲,職業是

$,愛好是$`

);}//靜態成員,就是通過建構函式本身新增的成員,不通過this,靜態成員只能通過建構函式本身來訪問,例項化的物件不能訪問靜態成員

student.*** =

'男';

console.

log(student.***)

//男console.

log(student.uname)

//undefined 不能通過建構函式來訪問例項成員

//stu1稱為例項化的物件,例項成員只能通過例項化的的物件來訪問

var stu1 =

newstudent

('tommy',18

,'student'

,'play computer games');

stu1.

intr()

;//我是tommy,我18歲,職業是student,愛好是play computer games

var stu2 =

newstudent

('tony',18

,'student'

,'basketball');

stu2.

intr()

;//我是tony,我18歲,職業是student,愛好是basketball

//stu1 == stu2; false,因為位址不同

console.

log(stu1.intr == stu2.intr)

;// true,因為兩個的intr方法都繼承自同乙個地方

student.prototype.school =

'航高'

;//為stu1和stu2新增自有屬性school,即使stu1的school屬性的屬性值與父親student的school屬性的屬性值相同,school也是stu1的自有屬性,若沒有stu1.school = '航高'這句**,school就不是stu1的自有屬性

// stu1.school = '航高';

stu2.school =

'四中'

;//鑑別自有屬性

console.

log(stu2.

hasownproperty

('school'))

;//true

//刪除stu1的name屬性;

delete stu1.uname;

console.

log(stu1)

//刪除原型上的age屬性;

delete student.prototype.age;

console.

log(student)

//判斷物件的某個屬性是否為自有屬性

var stu3 =

newstudent

('tony',18

,'student'

,'basketball');

function

check

(obj,pname)

else

if(obj.pname != undefined)

else

}check

(stu3,

'school');

// var newstu = new student('tommy',18,'student','play computer games')

// newstu.intr()

// console.log(new student('tommy',18,'student','play computer games'))

// //stu1是乙個物件

// var stu1 = new student('tommy',18,'student','play computer games');

// var stu2 = new student('tommy',18,'student','play computer games');

// function teacher(uname,age)

// var teach1 = new teacher();

遇到的有關js繼承和原型鏈的乙個問題

function a var1 a.prototype var b new a b.var1 2 var c new a console.log b.var1 輸出2 console.log c.var1 輸出undefined function a a.prototype var b new a ...

js的原型和原型鏈

所有引用型別 函式,陣列,物件 都擁有 proto 屬性 隱式原型 所有函式擁有prototype屬性 顯式原型 僅限函式 原型物件 擁有prototype屬性的物件,在定義函式時就被建立 建立建構函式 function human hname,hage human.prototype.intr f...

js的原型和原型鏈

1.首先 先搞清楚原型 person是乙個建構函式,我們new了乙個例項person function person var person new person person.name 大柱 console.log person.name 大柱prototype 每個函式都有乙個 prototype...