Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Last active August 29, 2015 14:02
Show Gist options
  • Save jikeytang/441222c0a9747b8ff015 to your computer and use it in GitHub Desktop.
Save jikeytang/441222c0a9747b8ff015 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140620-题目1
用代码实现函数B继承函数A里边的全部属性与方法,且覆盖函数A其中的sayName方法。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@ljkfgh2008
Copy link

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        #div1{
            width: 200px;
            height: 200px;
            background: black;
            position: relative;
        }
        #div2{
            width: 200px;
            height: 200px;
            background: red;
            position: relative;
        }
    </style>
    <script type="text/javascript">
        window.onload = function(){
            new Drop('div1');
            new LimtDrag('div2');
        }

        function Drop(id) {
            var _this = this;
            this.oDiv = document.getElementById(id);

            this.oDiv.onmousedown = function(){
                _this.fuDown();
            }
        }

        Drop.prototype.fuDown = function(){
                this.oDiv.style.left = '100px';
        }

        function LimtDrag(id){
            Drop.call(this,id);
        }

        for (var i in Drop.prototype) {
            LimtDrag.prototype[i] = Drop.prototype[i];
        };

        LimtDrag.prototype.fuDown = function(){
            this.oDiv.style.left = '200px'; 
        }

    </script>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
</body>
</html>

@hjzheng
Copy link

hjzheng commented Jun 23, 2014

function A(name){
    this.name = name;
}

A.prototype.sayName = function(){
    console.log("this is A sayName method");
    console.log(this.name);
}

function B(name){
    A.call(this, name);
}


B.prototype = new A();

B.prototype.sayName = function(){
    console.log("this is B sayName method");
    console.log(this.name);
}

var b = new B("b");
b.sayName();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment