js语法笔记3

<!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



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值