Pada artikel ini, kita akan berbicara tentang topik yang menarik dan agak membingungkan. Ini adalah kata kunci ‘ini’ dalam bahasa pemrograman Javascript. Show Singkatnya, Dalam JavaScript, kata kunci ini mengacu pada suatu objek. Objek mana yang bergantung pada bagaimana ini dipanggil (digunakan atau dipanggil). Dengan kata lain, this mereferensikan objek yang sedang memanggil fungsi. misalkan Anda memiliki objek bernama ‘penghitung’ yang memiliki satu metode bernama next() , saat Anda mengakses metode next() , ia mengembalikan nilai berikutnya dari nilai saat ini menggunakan objek this . let counter = { Di dalam metode next(), kata kunci ini mereferensikan objek hitungan. Cara kerjanya adalah dengan mendapatkan nilai objek hitungan saat ini menggunakan this , dan mengembalikan nilai berikutnya. Dan lihat kode berikut untuk memanggil metode tersebut: counter.next() Untuk contoh lebih lanjut, lihat kode di bawah ini: const test = { Menurut Anda apa yang akan dikembalikan kode jika saya memanggil metode func? Bagi yang menjawab 42, Anda benar. Karena metode func mengembalikan nilai objek prop dan dengan itu, Anda mendapatkan 42 sebagai hasilnya. Sekarang Anda mungkin bertanya, “lalu mengapa saya harus membuat metode baru hanya untuk memanggil objek, mengapa tidak memanggil objek itu sendiri saja?” Nah, dalam pemrograman ada istilah yang menjelaskan bahwa kita hanya bisa membuat sesuatu sesuai kegunaannya. Mungkin untuk contoh di atas, agak berlebihan untuk menerapkan istilah ini, tetapi untuk kode yang lebih canggih dan kompleks, Anda perlu menerapkan istilah ini. 2 kode di atas adalah contoh penggunaan kata kunci ini dalam sebuah objek. Global ContextIn the global execution context (outside of any function), // In web browsers, the window object is also the global object:Function Context Dalam konteks eksekusi global (di luar fungsi apa pun), ini mengacu pada objek global. function f1() {Class Context Perilaku ini di kelas dan fungsi serupa, karena kelas adalah fungsi di bawah tenda. Tetapi ada beberapa perbedaan dan peringatan. Di dalam konstruktor kelas, ini adalah objek biasa. Semua metode non-statis di dalam kelas ditambahkan ke prototipe ini: class Car { Dengan kode di atas, Anda dapat mengakses nama dan tahun saat ini (bergantung pada instance yang dipanggil) dari metode. Dengannya, kita bisa lebih leluasa dalam membuat dan memanggil konstruktor di kelas karena dengan ini membuat penulisan lebih mudah dipahami dan tidak membingungkan. In this guide, you'll learn about the basic components needed to create and use a packaged JavaScript action. To focus this guide on the components needed to package the action, the functionality of the action's code is minimal. The action prints "Hello World" in the logs or "Hello [who-to-greet]" if you provide a custom name. This guide uses the GitHub Actions Toolkit Node.js module to speed up development. For more information, see the actions/toolkit repository. Once you complete this project, you should understand how to build your own JavaScript action and test it in a workflow. To ensure your JavaScript actions are compatible with all GitHub-hosted runners (Ubuntu, Windows, and macOS), the packaged JavaScript code you write should be pure JavaScript and not rely on other binaries. JavaScript actions run directly on the runner and use binaries that already exist in the runner image. Warning: When creating workflows and actions, you should always consider whether your code might execute untrusted input from possible attackers. Certain contexts should be treated as untrusted input, as an attacker could insert their own malicious content. For more information, see "." PrerequisitesBefore you begin, you'll need to download Node.js and create a public GitHub repository.
Create a new file named This file defines the The actions toolkit is a collection of Node.js packages that allow you to quickly build JavaScript actions with more consistency. The toolkit The toolkit also offers a The toolkit offers more than the At your terminal, install the actions toolkit Now you should see a Writing the action codeThis action uses the toolkit to get the GitHub Actions provide context information about the webhook event, Git refs, workflow, action, and the person who triggered the workflow. To access the context information, you can use the Add a new file called If an error is thrown in the above Creating a READMETo let people know how to use your action, you can create a README file. A README is most helpful when you plan to share your action publicly, but is also a great way to remind you or your team how to use the action. In your
Commit, tag, and push your action to GitHubGitHub downloads each action run in a workflow during runtime and executes it as a complete package of code before you can use workflow commands like From your terminal, commit your It's best practice to also add a version tag for releases of your action. For more information on versioning your action, see "." Checking in your
Testing out your action in a workflowNow you're ready to test your action out in a workflow. When an action is in a private repository, the action can only be used in workflows in the same repository. Public actions can be used by workflows in any repository. Example using a public actionThis example demonstrates how your new public action can be run from within an external repository. Copy the following YAML into a new file at When this workflow is triggered, the runner will download the Example using a private actionCopy the workflow code into a .github/workflows/main.yml From your repository, click the Actions tab, and select the latest workflow run. Under Jobs or in the visualization graph, click A job to say hello. You should see "Hello Mona the Octocat" or the name you used for the Template repositories for creating JavaScript actionsGitHub provides template repositories for creating JavaScript and TypeScript actions. You can use these templates to quickly get started with creating a new action that includes tests, linting, and other recommended practices. |