用下面给定的方法构造一个对象.
方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast).
所有有参数的方法只接受一个字符串参数.
所有的方法只与实体对象交互.
var Person = function(firstAndLast) {
var name = firstAndLast;
this.getFirstName = function() {
var all = name.split(" ");
return all[0];
}
this.getLastName = function() {
var all = name.split(" ");
return all[1];
}
this.getFullName = function() {
return name;
}
this.setFirstName = function(firstAndLast) {
var all = name.split(" ");
all[0] = firstAndLast;
name = all[0] + " " + all[1];
}
this.setLastName = function(firstAndLast) {
var all = name.split(" ");
all[1] = firstAndLast;
name = all[0] + " " + all[1];
}
this.setFullName = function(firstAndLast) {
name = firstAndLast;
}
};
var bob = new Person('Bob Ross');
bob.getFullName();
// 参考文献
http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html