Cảm ơn đã phản ứng kịp thời. Tôi đã giúp xem xét lại những gì tôi đang làm vì mã tôi chia sẻ với bạn là OK. Nhưng tôi nhận ra một điều - vấn đề không phải do chúng nằm trong các dự án khác nhau - thực tế là tôi đã chia sẻ Parent. js vào dự án chứa Child. js. Đó là vì các tham chiếu vòng tròn. Mặc dù tài liệu TestComplete cho biết Javascript hỗ trợ các tham chiếu vòng tròn, nhưng có một nhược điểm ở đây
Câu hỏi ví dụ của tôi không đề cập đến ngữ cảnh đầy đủ - đây là mã mà tôi gặp sự cố
CHA MẸ. JS
// In Parent.js - In Project 1, but shared into Project 2 using Add Existing Item... var child = require("Child") class ParentApp { constructor(x) { this.name = x; } } function initializeGlobal() { var childObj = new child.ChildClass("Me", 3); } module.exports = {ParentClass: ParentApp}TRẺ EM. JS
// In Child.js - in Project 2 - also shared into Project 1 var parent = require("Parent") class ChildApp extends parent.ParentClass { constructor(y, z) { super(y); this.age = z; } } function test() { var myChild = new ChildApp('Bill', 25); Log.Message(myChild.age); } module.exports = { ChildClass: ChildApp }Điều đang xảy ra là ở Parent. js khi lần đầu tiên. var con = yêu cầu("Con"). đã được gọi, yêu cầu đã cố tải Con. JS - và trong trường hợp này đã cố gắng thực thi "lớp ChildApp mở rộng cha mẹ. ParentClass" đã gây ra lỗi. Điều này là do cha mẹ. ParentClass chưa được tải. Nếu tôi gỡ bỏ phần mở rộng thì tất cả đều ổn
Vì vậy, những gì tôi đã làm là cấu trúc lại mã để hàm "initializeGlobal()" nằm trong một tập lệnh phổ biến khác để tránh tham chiếu vòng tròn. Có vẻ như tham chiếu vòng tròn không thành công với phần mở rộng
ES6 là một tiêu chuẩn JavaScript. Với sự trợ giúp của ES6, chúng ta có thể tạo các mô-đun bằng JavaScript. Trong một mô-đun, có thể có các lớp, hàm, biến và đối tượng. Để cung cấp tất cả những thứ này trong một tệp khác, chúng tôi có thể sử dụng xuất và nhập. Xuất và nhập là các từ khóa được sử dụng để xuất và nhập một hoặc nhiều thành viên trong một mô-đun
Xuất khẩu. Bạn có thể xuất một biến bằng cách sử dụng từ khóa export trước phần khai báo biến đó. Bạn cũng có thể xuất một hàm và một lớp bằng cách thực hiện tương tự
- Cú pháp cho biến. export let variable_name;
- Cú pháp cho chức năng. export function function_name() { // Statements }
- Cú pháp cho lớp. export class Class_Name { constructor() { // Statements } }
- ví dụ 1. Tạo một tệp có tên export. js và viết mã bên dưới vào tệp đó
export let num_set = [1, 2, 3, 4, 5];
exportdefault export function function_name() { // Statements } 0 export function function_name() { // Statements } 1
export function function_name() { // Statements } 2_______3_______3_______3_______4export function function_name() { // Statements } 5
export function function_name() { // Statements } 6
export function function_name() { // Statements } 8
export function function_name() { // Statements } 2_______4_______0
export class Class_Name { constructor() { // Statements } } 1export class Class_Name { constructor() { // Statements } } 2_______4_______3export class Class_Name { constructor() { // Statements } } 4 export class Class_Name { constructor() { // Statements } } 5
export function function_name() { // Statements } 2_______3_______6
export function function_name() { // Statements } 6
- ví dụ 2. Trong ví dụ này, chúng tôi xuất bằng cách chỉ định các thành viên của mô-đun ở cuối tệp. Chúng tôi cũng có thể sử dụng bí danh trong khi xuất bằng từ khóa as
export class Class_Name { constructor() { // Statements } } 9
exportdefault export function function_name() { // Statements } 0 export function function_name() { // Statements } 1
export function function_name() { // Statements } 2_______3_______3_______3_______4export function function_name() { // Statements } 5
export function function_name() { // Statements } 6
// You can also use an alias while importing a member. import Greeting as Greet from "./export.js";1
export function function_name() { // Statements } 2_______4_______0
export class Class_Name { constructor() { // Statements } } 1export class Class_Name { constructor() { // Statements } } 2_______4_______3export class Class_Name { constructor() { // Statements } } 4 export class Class_Name { constructor() { // Statements } } 5
export function function_name() { // Statements } 2_______3_______6
export function function_name() { // Statements } 6
// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";3
Ghi chú. Xuất khẩu mặc định nên được chỉ định ở đây
Nhập khẩu. Bạn có thể nhập một biến bằng cách sử dụng từ khóa nhập. Bạn có thể chỉ định một trong tất cả các thành viên mà bạn muốn nhập từ tệp JavaScript
- cú pháp. import member_to_import from “path_to_js_file”;// You can also use an alias while importing a member. import Greeting as Greet from "./export.js";// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";
- ví dụ 1. Tạo một tệp có tên nhập khẩu. html và viết đoạn mã dưới đây vào tệp đó
// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";4
// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";6 // If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";7// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";8_______42_______9export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______42_______5export let num_set = [1, 2, 3, 4, 5];3export let num_set = [1, 2, 3, 4, 5];0
export class Class_Name { constructor() { // Statements } } 1// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5export let num_set = [1, 2, 3, 4, 5];7export let num_set = [1, 2, 3, 4, 5];8export let num_set = [1, 2, 3, 4, 5];7export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______90_______2export let num_set = [1, 2, 3, 4, 5];3export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______42_______5 7export let num_set = [1, 2, 3, 4, 5];0
export class Class_Name { constructor() { // Statements } } 1// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5export1 export2// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";8export4export let num_set = [1, 2, 3, 4, 5];0
export7export8
export7default0
export7default2
export7default4
export7default6
export7default8
export class Class_Name { constructor() { // Statements } } 1 2export1export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______90_______2 7export let num_set = [1, 2, 3, 4, 5];0
2_______42_______6export let num_set = [1, 2, 3, 4, 5];0
- ví dụ 2
// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";4
// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";6 // If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";7// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";8_______42_______9export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______42_______5export let num_set = [1, 2, 3, 4, 5];3export let num_set = [1, 2, 3, 4, 5];0
export class Class_Name { constructor() { // Statements } } 1// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5export let num_set = [1, 2, 3, 4, 5];7export let num_set = [1, 2, 3, 4, 5];8export let num_set = [1, 2, 3, 4, 5];7export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______90_______2export let num_set = [1, 2, 3, 4, 5];3export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______42_______5 7export let num_set = [1, 2, 3, 4, 5];0
export class Class_Name { constructor() { // Statements } } 1// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";5export1 export2// If you want to import all the members but don’t // want to Specify them all then you can do that using // a ' * ' star symbol. import * as exp from "./export.js";8export4export let num_set = [1, 2, 3, 4, 5];0
export7export function function_name() { // Statements } 43
export7export function function_name() { // Statements } 46
export7export function function_name() { // Statements } 48
export7export function function_name() { // Statements } 50
export7export function function_name() { // Statements } 52
export7default8
export class Class_Name { constructor() { // Statements } } 1 2export1export let num_set = [1, 2, 3, 4, 5];0
export function function_name() { // Statements } 2_______90_______2 7export let num_set = [1, 2, 3, 4, 5];0
2_______42_______6export let num_set = [1, 2, 3, 4, 5];0
- đầu ra. Đầu ra sẽ giống nhau, nhập cùng một tệp
Ghi chú. Thành viên mặc định phải được nhập trước và sau đó không có thành viên mặc định nào trong dấu ngoặc nhọn