1. 程式人生 > 其它 >【JavaScript練習】模組化

【JavaScript練習】模組化

模組化

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <
title>Document</title> </head> <body> <script> // 完成函式 createModule, 呼叫之後滿足如下要求: // 1、 返回一個物件 // 2、 物件的 greeting 屬性值等於 str1, name 屬性值等於 str2 // 3、 物件存在一個 sayIt 方法, 該方法返回的字串為 greeting屬性值 + ', ' + name屬性值 function createModule(str1, str2) {
function test(str1, str2) { this.greeting = str1; this.name = str2; this.sayIt = function() { return this.greeting + ',' + this.name; } } return new test(str1, str2); } var
res = createModule("hello", "ben"); console.log(res); </script> </body> </html>

執行結果:
在這裡插入圖片描述