批改狀態(tài):合格
老師批語:面向?qū)ο缶幊? 需要轉(zhuǎn)變一下思維模式
構(gòu)造函數(shù),專用于創(chuàng)建對象
首字母要大寫,this 代表通過構(gòu)造函數(shù)要創(chuàng)建的新對象
var Createboj = function (){ this.placeholder = '大于六十分以上為及格,低于六十分則為不及格'; this.grade = function (value){ var res = '' ; if(value >= 60){ return res = '成績合格'; }else{ return res = '成績不合格'; } }; return this ; }; var obj1 = new Createboj(); console.log(obj1.placeholder); console.log(obj1.grade(90));
點擊 "運(yùn)行實例" 按鈕查看在線實例
效果如下:
構(gòu)造函數(shù)的prototype原型屬性添加成員,實現(xiàn)數(shù)據(jù)在實例間共享
<script> var Createboj = function (){ this.placeholder = '大于六十分以上為及格,低于六十分則為不及格'; this.grade = function (value){ var res = '' ; if(value >= 60){ return res = '成績合格'; }else{ return res = '成績不合格'; } }; return this ; }; var obj1 = new Createboj(); console.log(obj1.placeholder); console.log(obj1.grade(90)); //向原型對象添加成員,實現(xiàn)數(shù)據(jù)在實例間共享 Createboj.prototype.age = '我十九歲了'; console.log(obj1.age); </script>
點擊 "運(yùn)行實例" 按鈕查看在線實例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號