码迷,mamicode.com
首页 > 其他好文 > 详细

练习prototype的实现

时间:2016-08-21 18:25:08      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

这个BLOG不错,我练习了一下代码。

感觉他是个高手。

========================

http://www.w3cfuns.com/notes/17398/35b250e9b392675c44f4f0cd833b72c8.html

===================================

<html>
<head>
<title>Test</title>
<script>
    var girl = {
        name: 小雪
    };
    var boy = {
        name: 小帅,
        pay: function() {
            console.log(花了一千元);
        }
    };
    Object.setPrototypeOf(girl, boy);
    girl.pay();
    var a = {
        fn1: function() {
            console.log(1);
        }
    };
    var b = {
        fn2: function() {
            console.log(2);
        }
    };
    var c = {
        fn3: function() {
            console.log(3);
        }
    };
    var d = {
        fn4: function() {
            console.log(4);
        }
    };
    Object.setPrototypeOf(d, c);
    Object.setPrototypeOf(c, b);
    Object.setPrototypeOf(b, a);
    d.fn1();
    d.fn2();
    d.fn3();
    d.fn4();
    
    var a = {
        sayName: function() {
            alert(this.name);
        }
    };
    var laoyao = {
        name: laoyao
    };
    Object.setPrototypeOf(laoyao, a);
    //laoyao.sayName();
    var laoyao = {
        name: laoyao,
        sayName: function() {
            alert(this.name);
        }
    };
    //var fenshen = {};
    //Object.setPrototypeOf(fenshen, laoyao);
    var fenshen = Object.create(laoyao);
    console.log(fenshen);
    //fenshen.sayName();
    
    var createPerson = function(name) {
        return {
            name: name,
            sayName: function() {
                alert(this.name);
            }
        };
    };
    var laoyao = createPerson(laoyao);
    //laoyao.sayName();
    var createPerson = function(name) {
        var o = {};
        o.name = name;
        
        var proto = {
            sayName: function() {
                alert(this.name);
            }
        };
        Object.setPrototypeOf(o, proto);
        return o;
    };
    var laoyao = createPerson(laoyao);
    //laoyao.sayName();
    var createPerson = function(name) {
        var o = {};
        o.name = name;
        Object.setPrototypeOf(o, createPerson.proto);
        return o;
    };
    createPerson.proto = {
        sayName: function() {
            alert(this.name);
        }
    };
    var laoyao = createPerson(laoyao);
    laoyao.sayName();
</script>
</head>
<body>
test
</body>
</html>

技术分享

练习prototype的实现

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5793185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!