Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Vấn đề về class và function trong es6

    Hỏi Đáp
    0
    10
    1731
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      MDB Relo last edited by Vũ

      Chào ae. Mình mới chuyển từ C# qua thằng Nodejs này thì thấy bỡ ngỡ quá. Nay mới mò ra thằng es6 có cải tiến thêm class làm cho node js hướng đối tượng hơn nhưng mình lại bị một lỗi như dưới:

      VD1: code báo lỗi "taskCompleted is not defined"

      class test {
      
          taskCompleted(task) {
              console.log("ok");
          }
      
         static ex() {
              var http = new chilkat.Http();
              var success;
              success = http.UnlockComponent("Anything for 30-day trial");
              var task = http.QuickGetAsync("http://www.wikipedia.org/");
              task.Run(taskCompleted);
          }
      }
      
      test.ex();
      

      VD2: Code chạy ngon lành khi không dùng class

      function taskCompleted(task) {
              console.log("ok");
          }
      
          function ex() {
              var http = new chilkat.Http();
              var success;
              success = http.UnlockComponent("Anything for 30-day trial");
              var task = http.QuickGetAsync("http://www.wikipedia.org/");
              task.Run(taskCompleted);
          }
      
      ex();
      

      Cho mình hỏi là mình đang bị sai ở chỗ nào vậy. Đọc bao nhiêu tài liệu rồi mà không tìm ra cách xử lý vụ này mong ae giúp mình với. Cảm ơn

      1 Reply Last reply Reply Quote 0
      • H
        Hải Nguyễn215 last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • H
          Hải Nguyễn215 last edited by

          sorry, mình ko đọc kỹ là bạn dùng class.
          mình cũng chưa dùng class trong js bao giờ ^^.
          có tus này, tham khảo thử nhé.
          https://toidicode.com/classes-trong-es6-340.html

          1 Reply Last reply Reply Quote 0
          • M
            MDB Relo last edited by Vũ

            Cảm ơn mọi người. Vấn đề của mình là static. Mình bỏ static đi sau đó dùng this để trỏ đến function là được. Đây là code của mình sau khi fix

            class test {
            
            taskCompleted(task) {
                console.log("ok");
            }
            
            ex() {
                var http = new chilkat.Http();
                var success;
                success = http.UnlockComponent("Anything for 30-day trial");
                var task = http.QuickGetAsync("http://www.wikipedia.org/");
                task.Run(this.taskCompleted);
            }
            }
            
            var ctest = new test();
            ctest.ex();
            
            Nguyen Hien 1 Reply Last reply Reply Quote 2
            • Vũ
              Vũ Global Moderator last edited by

              Dùng con trỏ this là được. Đã format lại post của bạn theo chuẩn markdown nhé

              Tech-nông
              Email: [email protected]
              Profile: about.me/vunb
              Github: github.com/vunb
              Twitter: twitter.com/nhubaovu

              1 Reply Last reply Reply Quote 1
              • CodeConCat
                CodeConCat last edited by

                Theo mình nghĩ, NodeJs k phải để bạn làm OOP đâu, bạn nên đổi mindset khi làm với NodeJS đi.
                Nếu bạn quen làm OOP thì nên vẫn làm C#

                codeconcat

                K 1 Reply Last reply Reply Quote 1
                • K
                  kevin3489 @CodeConCat last edited by

                  @CodeConCat "Theo mình nghĩ, NodeJs k phải để bạn làm OOP đâu, bạn nên đổi mindset khi làm với NodeJS đi.", JS OOP bình thường mà. Ứng dụng lớn phải thiết kế theo OOP chứ.

                  1 Reply Last reply Reply Quote 0
                  • CodeConCat
                    CodeConCat last edited by

                    Ý mình là bạn có thể dùng Js theo OOP, nhưng bạn không nên, giống như việc tàu lửa có thể chạy trên đường nhựa được, nhưng vậy sẽ không đúng mục đích ban đầu, đó là theo ý kiến cá nhân của mình.

                    Bạn trên mới chuyển sang NodeJS, mình nghĩ không nên tập trung vào ứng dụng lớn, mà nên nghiên cứu những cái cơ bản.

                    codeconcat

                    H 1 Reply Last reply Reply Quote 1
                    • H
                      Hải Nguyễn215 @CodeConCat last edited by

                      @CodeConCat theo bác OOP trong JS thì có nhưng điểm hay, ko hay như thế nào ạ??

                      1 Reply Last reply Reply Quote 0
                      • Nguyen Hien
                        Nguyen Hien @MDB Relo last edited by

                        @MDB-Relo Nếu quen C# thì sử dụng TypeScript thử :3
                        https://www.typescriptlang.org/

                        Sent from Rodgers Zone!!!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        $(document).ready(function () { app.coldLoad(); }); }