js---面向对象OOP浅谈
对象化编程-------简单地去理解就是把javascript能涉及到的范围分成各种对象,对象下面再次划分对象。编程出发点多是对象,或者说基于对象。所说的对象既包含变量,网页,窗口等等
- 对象的含义
- 属性-------对象的某些特定的性质
- 方法-------对象能做的事情
- 事件-------能响应发生在对象上的事情
-
Number
-
String
-
Array
-
Math
-
Date
这边我只是简单地罗列出来部分,具体的可以参考http://www.w3school.com.cn/js/js_obj_intro.asp
不过我这边还是想讲一下比较流行的一道前端面试题,也是我当初来百度面试的时候问我的(题目的来源好像是方荣大侠的某个web前端研发工程师编程能力成长之路的文档里面的)
废话少说------出题:
“输出字符串--今天是星期几”
答案1:
var _str = ""; var _today = new Date().getDay(); if(_today == 0){ _str = "今天是星期日"; }else if(_today == 1){ _str = "今天是星期一"; }else if(_today == 2){ _str = "今天是星期二"; }else if(_today == 3){ _str = "今天是星期三"; }else if(_today == 4){ _str = "今天是星期四"; }else if(_today == 5){ _str = "今天是星期五"; }else if(_today == 6){ _str = "今天是星期六"; }
答案2:
var _str ="今天是星期";
var _today=new Date().getDay(); switch(_today){ case 0: _str += "日"; break; case 1: _str += "一"; break; case 2: _str += "二"; break; case 3: _str += "三"; break; case 4: _str += "四"; break; case 5: _str += "五"; break; case 6: _str += "六"; break; }
答案3:
var _arr = new Array("日","一","二","三","四","五","六"); var _today = new Date().getDay(); var _str = "今天是星期"+_arr[_today ];
答案4:
var _str = "今天是星期"+"日一二三四五六".charAt(new Date().getDay());
3.
var people ={};
people.name = "steven";
people.age = 23;
people.getName = function(){
return "People's name is "+ this.name;
};
console.log(people.getName()); //People's name is steven
console.log(people.age); //23
不好的地方就是:在创建多个对象的场景下会产生 很多冗余的代码,耦合度不高
function makePeople(name,age,job){
var _obj = {};
_obj.name = name;
_obj.age =age;
_obj.job = job;
_obj.getName = function(){
return "People's name is "+ this.name;
}
return _obj;
}
var webdesigner = makePeople("steven",23,"wendesigner");
console.log(webdesigner.getName ); //People's name is steven
console.log(webdesigner.job) //wendesigner
不好的地方就是:实例化比较频繁
function People(){};
People.prototype = {
constructor :People,
name:"steven",
age:23,
job:"webdesigner",
getName:function(){
return "People's name is "+this.name;
}
}
var webdesign = new People();
var carman = new People();
console.log(webdesign.getName()); //People's name is steven
console.log(carman.getName()); //People's name is steven
不好的地方就是:初始化参数不支持传递,还有就是原型的所有属性和方法会被所有的实例共享
function People(name.age.job){
this.name = name;
this.age = age; this.job = job; }; People.prototype = { constructor:People, getName: function(){ return "People's name is "+this.name; } } var webdesigner = new People("steven",23,"webdesigner"); var carman = new People("zyc",24,"carman"); console.log(webdesigner.getName()) //People's name is steven console.log(carman.getName()) //People's name is zyc
不好的地方就是:对象的属性和方法也多是公用的
(function(){
var name ="";
People = function(val){
name = val;
};
People.prototype ={
constructor:People,
getName:function(){
return "People's name is "+ name ;
}
};
})();
var webdesigner = new People("steven");
console.log(webdesigner.name); //undefined
console.log(webdesigner.getName()); //People's name is steven
var carman= new People("zyc");
console.log(carman.name); //undefined
console.log(carman.getName()); //People's name is zyc
不好的地方就是:初级程度代码不是很让人理解
推荐阅读
-
PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】
-
浅谈Java面向对象思想
-
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
-
Python面向对象程序设计OOP入门教程【类,实例,继承,重载等】
-
Java面向对象(五):OOP三大特性之多态 — final 关键字
-
06_JavaSE之OOP--面向对象(final、抽象类、接口)
-
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
-
oop面向对象【接口、多态】
-
漫谈面向对象与面向主体(OOP vs AOP) AOPOOP编程OO数据结构
-
js---面向对象OOP浅谈