复制代码 代码如下: Object.defineProperty( chenhao, "birth_year", { get: function() { var d = new Date(); var y = d.getFullYear(); return ( y - this.age ); }, set: function(year) { var d = new Date(); var y = d.getFullYear(); this.age = y - year; } } );
复制代码 代码如下:var chenhao = { name: "Chen Hao", email: "haoel@hotmail.com", website: "http://jb51.net", age: 100, get birth_year() { var d = new Date(); var y = d.getFullYear(); return ( y - this.age ); }, set birth_year(year) { var d = new Date(); var y = d.getFullYear(); this.age = y - year; }
复制代码 代码如下: //列出对象的属性. function listProperties(obj) { var newLine = "<br />"; var names = Object.getOwnPropertyNames(obj); for (var i = 0; i < names.length; i++) { var prop = names[i]; document.write(prop + newLine);
// 列出对象的属性配置(descriptor)动用getOwnPropertyDescriptor函数。 var descriptor = Object.getOwnPropertyDescriptor(obj, prop); for (var attr in descriptor) { document.write("..." + attr + ": " + descriptor[attr]); document.write(newLine); } document.write(newLine); } }
listProperties(chenhao);
call,apply, bind 和 this 关于Javascript的this指针,和C++/Java很类似。 我们来看个示例:(这个示例很简单了,我就不多说了)
复制代码 代码如下:print.call(a, "a"); // this => a, output "10 - a" print.call(b, "b"); // this => b, output "20 - b"
print.apply(a, ["a"]); // this => a, output "10 - a" print.apply(b, ["b"]); // this => b, output "20 - b"
但是在bind后,this指针,可能会有不一样,但是因为Javascript是动态的。如下面的示例
复制代码 代码如下:var p = print.bind(a); p("a"); // this => a, output "10 - a" p.call(b, "b"); // this => a, output "10 - b" p.apply(b, ["b"]); // this => a, output "10 - b"
Person.sayHello = function () { var hello = "<p>Hello, I am "+ this.name + ", <br>" + "my email is: " + this.email + ", <br>" + "my website is: " + this.website; document.write(hello + "<br>"); }
//重载SayHello方法 Student.sayHello = function (person) { var hello = "<p>Hello, I am "+ this.name + ", <br>" + "my email is: " + this.email + ", <br>" + "my website is: " + this.website + ", <br>" + "my student no is: " + this. no + ", <br>" + "my departent is: " + this. dept; document.write(hello + "<br>"); } //再次调用 Student.sayHello();
复制代码 代码如下://新版的重载SayHello方法 Student.sayHello = function (person) { Object.getPrototypeOf(this).sayHello.call(this); var hello = "my student no is: " + this. no + ", <br>" + "my departent is: " + this. dept; document.write(hello + "<br>"); }
Person.prototype.sayHello = function(){ var hello = "Hello, I am "+ this.name + ", <br>" + "my email is: " + this.email + ", <br>" + "my website is: " + this.website; return hello; };
function Student(name, email, website, no, dept){ var proto = Object.getPrototypeOf; proto(Student.prototype).constructor.call(this, name, email, website); this.no = no; this.dept = dept; }
//重载sayHello() Student.prototype.sayHello = function(){ var proto = Object.getPrototypeOf; var hello = proto(Student.prototype).sayHello.call(this) + "<br>"; hello += "my student no is: " + this. no + ", <br>" + "my departent is: " + this. dept; return hello; };
var me = new Student( "Chen Hao", "haoel@hotmail.com", "http://jb51.net", "12345678", "Computer Science" ); document.write(me.sayHello());