js语法笔记3

xiaoxiao2021-02-28  117

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body style="background: rgba(199,237,204,1)"> <pre id="container"> </pre> <pre> </pre> </body> <script type="text/javascript"> function print(txt) { document.getElementById("container").innerHTML += ('\n') + txt; } document.body.onclick = function () { window.location.reload() } console.log = print; console.log("面向对象的程序设计") eval('for(var i in [0,1,2]){print(i);}'); var person = {}; Object.defineProperty(person, "name", { writable: false, configurable: false, value: "xiaoming" }); print(person.name)//xiaoming person.name = "小李"; print(person.name)//xiaoming delete person.name print(person.name)//xiaoming function Person(name, age) { this.name = name; this.age = age; //每一个Person对象独有一个sayName函数 this.sayName = function () { return this.name; } } //所有Person的对象公用同一个sayAge函数 Person.prototype.sayAge = function () { return this.age; }; var p1 = new Person("xm", 10); var p2 = new Person('小李', 12); print(p1.sayName())//xm print(p2.sayName())// 小李 print(p1.sayAge())//10 print(p2.sayAge())// 12 print(p1.constructor == Person)//true print(p2.constructor == Person)//true print(p1 instanceof Person)//true print(p1.sayName == p2.sayName)//false print(p1.sayAge == p2.sayAge)//true p1.sayAge = function () { return 'my age is ' + this.age; }; print(p1.sayAge());//my age is 10 delete p1.sayAge; print(p1.sayAge());//10 Person("小红", 13); print(window.sayName());//小红 print(p1.hasOwnProperty('sayAge'))//false p1.sayAge = function () { } print(p1.hasOwnProperty('sayAge'))//true print("sayAge" in p1)//true for (var prop in p1) { // name // age // sayName // sayAge print(prop) } print('.......') print(Object.keys(Person.prototype))//sayAge print(Object.keys(p1))//name,age,sayName,sayAge </script> </html>

面向对象的程序设计 0 1 2 xiaoming xiaoming xiaoming xm 小李 10 12 true true true false true my age is 10 10 小红 false true true name age sayName sayAge ....... sayAge name,age,sayName,sayAge

转载请注明原文地址: https://www.6miu.com/read-32303.html

最新回复(0)