CodeLab
Show Output
 
xxxxxxxxxx
27
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="utf-8">
5
    <title>JavaScript Call Methods</title>
6
</head>
7
<body>
8
    <script> 
9
    var objA = {
10
        name: "object A",
11
        say: function(greet) {
12
            document.write(greet + ", " + this.name);
13
        }
14
    }
15
    
16
    objA.say("Hi"); // Prints: Hi, object A
17
    document.write("<br>");
18
    
19
    var objB = {
20
        name: "object B"
21
    }
22
    
23
    /* The objB doesn't have say() method, but it can borrow it from objA */
24
    objA.say.call(objB, "Hello"); // Prints: Hello, object B
25
    </script>
26
</body>
27
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。