<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <p>2</p> <script src="myScript.js"></script> <script> person.printName(); </script> </body> </html>
var person = (function () { var name = 'vicin'; var age = 28; var printName = function () { console.log(name) } var printAge = function () { console.log(age); } return { printName: printName, printAge: printAge } })();
输出:vicin