Created
March 2, 2015 18:29
-
-
Save kitsuyui/b0c129ad340fe2282d63 to your computer and use it in GitHub Desktop.
「 JS のメソッドの貸し借り (apply 使うやつ ) 」を Python でやるには ref: http://qiita.com/kitsuyui/items/0482de343f88399464b7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
def __init__(self): | |
self.value = 1 | |
class B(object): | |
def __init__(self): | |
self.value = 2 | |
def show(self): | |
print(self.value) | |
a = A() | |
B.__dict__['show'](a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function A () { | |
this.value = 1; | |
} | |
function B () { | |
this.value = 2; | |
} | |
B.prototype.show = function show () { | |
console.log(this.value); | |
}; | |
var a = A(); | |
B.prototype.show.apply(a); // まるで「 B のメソッドである show を a が借りて使ってる」よう |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment