Tôi đang cố gắng thiết lập đăng nhập trên chương trình TypeScript của mình, sử dụng
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
5.Tuy nhiên, tôi không biết làm thế nào để truy xuất các tên chức năng bằng cách sử dụng phản xạ [thay vì gõ bằng tay].
Lý tưởng nhất là tôi muốn mô phỏng những gì tôi làm trong
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
6:public class Foo
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger[typeof[Foo]];
public Foo[]
{
}
public FooMethod[]
{
try {
logger.Logger.Log[this.GetType[], log4net.Core.Level.Trace, "Entering" + MethodBase.GetCurrentMethod[].Name, null];
// code
}
catch [e] {
logger.Logger.Log[this.GetType[], log4net.Core.Level.Debug, ex.Message, null];
}
finally {
logger.Logger.Log[this.GetType[], log4net.Core.Level.Trace, "Exiting" + MethodBase.GetCurrentMethod[].Name, null];
}
}
}
Làm thế nào tôi có thể làm điều này trong
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
7? Tất cả những gì tôi có thể làm là lấy tên lớp.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
Tên lớp trả về chính xác, nhưng các chức năng trả về "hàm".
Thuộc tính
9 chỉ đọc của đối tượng cho biết tên của hàm được chỉ định khi nó được tạo hoặc nó có thể là function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
0 hoặc function doSomething[] {}
doSomething.name; // "doSomething"
1 [một chuỗi trống] cho các hàm được tạo ẩn danh.function doSomething[] {}
doSomething.name; // "doSomething"
9 property indicates the function's name as specified when it was created, or it may be either function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
0 or function doSomething[] {}
doSomething.name; // "doSomething"
1 [an empty string] for functions created anonymously.function doSomething[] {}
doSomething.name; // "doSomething"
Thử nó
Lưu ý: Trong các triển khai không chuẩn, Pre-ES2015, thuộc tính
2 cũng là function doSomething[] {}
doSomething.name; // "doSomething"
3. In non-standard, pre-ES2015 implementations the function doSomething[] {}
doSomething.name; // "doSomething"
function doSomething[] {}
doSomething.name; // "doSomething"
2 attribute was function doSomething[] {}
doSomething.name; // "doSomething"
3 as well.Sự mô tả
Thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 của chức năng có thể được sử dụng để xác định chức năng trong các công cụ gỡ lỗi hoặc thông báo lỗi. Nó không có ý nghĩa ngữ nghĩa đối với chính ngôn ngữ.Thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 chỉ được đọc và không thể thay đổi bởi toán tử gán:function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
Để thay đổi nó, sử dụng
function doSomething[] {}
doSomething.name; // "doSomething"
6.Thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 thường được suy ra từ cách xác định hàm. Trong các phần sau, chúng tôi sẽ mô tả các cách khác nhau mà nó có thể được suy ra.Tuyên bố chức năng
Thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 trả về tên của một khai báo chức năng.function doSomething[] {}
doSomething.name; // "doSomething"
Tuyên bố chức năng xuất hiện mặc định
Tuyên bố
function doSomething[] {}
doSomething.name; // "doSomething"
9 xuất hàm dưới dạng khai báo thay vì biểu thức. Nếu tuyên bố là ẩn danh, tên là // -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
0.// -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
Hàm tạo chức năng
Các chức năng được tạo với trình xây dựng
// -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
1 có tên là "ẩn danh".new Function[].name; // "anonymous"
Biểu thức chức năng
Nếu biểu thức hàm được đặt tên, tên đó được sử dụng làm thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9.const someFunction = function someFunctionName[] {};
someFunction.name; // "someFunctionName"
Các biểu thức chức năng ẩn danh được tạo bằng cách sử dụng từ khóa
// -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
3 hoặc các hàm mũi tên sẽ có // -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
4 [một chuỗi trống] làm tên của chúng.[function [] {}].name; // ""
[[] => {}].name; // ""
Tuy nhiên, những trường hợp như vậy là rất hiếm - thông thường, để chỉ biểu thức ở nơi khác, biểu thức hàm được gắn vào một định danh khi nó được tạo [chẳng hạn như trong một khai báo biến]. Trong những trường hợp như vậy, tên có thể được suy ra, vì một vài tiểu mục sau đây chứng minh.
Một trường hợp thực tế trong đó tên không thể được suy ra là một hàm được trả về từ một hàm khác:
function getFoo[] {
return [] => {};
}
getFoo[].name; // ""
Khai báo và phương pháp biến
Các biến và phương thức có thể suy ra tên của một hàm ẩn danh từ vị trí cú pháp của nó.
const f = function [] {};
const object = {
someMethod: function [] {}
};
console.log[f.name]; // "f"
console.log[object.someMethod.name]; // "someMethod"
Điều tương tự áp dụng cho bài tập:
class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
0Khởi tạo và giá trị mặc định
Các chức năng trong các bộ khởi tạo [giá trị mặc định] của phá hủy, tham số mặc định, trường lớp, v.v., sẽ kế thừa tên của định danh bị ràng buộc là
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 của chúng.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
1Phương pháp tốc ký
class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
2Chức năng ràng buộc
// -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
6 tạo ra một hàm có tên là "ràng buộc" cộng với tên hàm.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
3Getter và setter
Khi sử dụng các thuộc tính của
// -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
7 và // -- someModule.js --
export default function [] {};
// -- main.js --
import someModule from "./someModule.js";
someModule.name; // "default"
8, "GET" hoặc "SET" sẽ xuất hiện trong tên hàm.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
4Lớp
Tên của một lớp theo cùng một thuật toán như các khai báo và biểu thức hàm.
class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
5Cảnh báo: JavaScript sẽ đặt thuộc tính
9 của hàm chỉ khi một hàm không có thuộc tính riêng gọi là function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9. Tuy nhiên, các thành viên tĩnh của các lớp sẽ được đặt làm thuộc tính riêng của hàm xây dựng lớp và do đó ngăn chặn function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 tích hợp được áp dụng. Xem một ví dụ dưới đây. JavaScript will set the function's function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 property only if a function does not have an own property called function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9. However, classes'
static members will be set as own properties of the class constructor function, and thus prevent the built-in function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 from being applied. See an example below.Biểu tượng là tên hàm
Nếu
new Function[].name; // "anonymous"
2 được sử dụng tên hàm và biểu tượng có mô tả, tên của phương thức là mô tả trong dấu ngoặc vuông.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
6Tài sản cá nhân
Các trường riêng và các phương thức riêng có hàm băm [
new Function[].name; // "anonymous"
3] như một phần của tên của họ.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
7Ví dụ
Nói tên hàm tạo của một đối tượng
Bạn có thể sử dụng
new Function[].name; // "anonymous"
4 để kiểm tra "lớp" của một đối tượng.class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
8Tuy nhiên, vì các thành viên tĩnh sẽ trở thành thuộc tính riêng của lớp, chúng tôi không thể có được tên lớp cho hầu như bất kỳ lớp nào có thuộc tính Phương thức tĩnh
new Function[].name; // "anonymous"
5:class Foo {
private static logger: log4javascript.Logger = log4javascript.getLogger[getName[Foo]];
constructor[] {
}
FooFunction[] {
try {
SymDataSource.logger.trace["Entering: " + getName[Foo.prototype.FooFunction]];
// code
} catch [e] {
SymDataSource.logger.debug["Exception: " + getName[Foo.prototype.FooFunction], e];
} finally {
SymDataSource.logger.trace["Exiting: " + getName[Foo.prototype.FooFunction]];
}
}
}
function getName[obj: any]: string {
if [obj.name] {
return obj.name;
}
var funcNameRegex = /function [.{1,}]\[/;
var results = [funcNameRegex].exec[[ obj].constructor.toString[]];
return [results && results.length > 1] ? results[1] : "";
}
9Với phương thức
new Function[].name; // "anonymous"
6 new Function[].name; // "anonymous"
7 không còn giữ tên lớp thực tế mà là một tham chiếu đến đối tượng hàm new Function[].name; // "anonymous"
5. Cố gắng để có được lớp new Function[].name; // "anonymous"
9 qua const someFunction = function someFunctionName[] {};
someFunction.name; // "someFunctionName"
0 sẽ không cho chúng ta tên lớp, mà thay vào đó là một tham chiếu đến phương thức lớp tĩnh. Thí dụ:function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
0Do sự tồn tại của các trường tĩnh,
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 cũng có thể không phải là một hàm.function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
1Nếu một lớp có một thuộc tính tĩnh được gọi là
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9, nó cũng sẽ trở nên có thể ghi được. Định nghĩa tích hợp trong trường hợp không có định nghĩa tĩnh tùy chỉnh chỉ được đọc:function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
2Do đó, bạn không thể dựa vào thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 tích hợp để luôn giữ tên của một lớp.Máy nén JavaScript và máy tính
CẢNH BÁO: Hãy cẩn thận khi sử dụng thuộc tính
9 với các biến đổi mã nguồn, chẳng hạn như các biến đổi được thực hiện bởi máy nén JavaScript [minifi] hoặc obfuscators. Các công cụ này thường được sử dụng như một phần của đường ống xây dựng JavaScript để giảm quy mô của một chương trình trước khi triển khai nó để sản xuất. Các phép biến đổi như vậy thường thay đổi tên của một hàm tại thời điểm xây dựng. Be careful when using the function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9 property with source-code transformations, such as those carried out by JavaScript compressors [minifiers] or obfuscators. These tools are often used as part of a JavaScript
build pipeline to reduce the size of a program prior to deploying it to production. Such transformations often change a function's name at build time.Mã nguồn như:
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
3có thể được nén vào:
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
4Trong phiên bản không nén, chương trình chạy vào nhánh Truthy và ghi lại "'foo' là một ví dụ của 'foo'" - trong khi, trong phiên bản nén, nó hoạt động khác nhau và chạy vào nhánh khác. Nếu bạn dựa vào thuộc tính
function someFunction[] {}
someFunction.name = 'otherFunction';
console.log[someFunction.name]; // someFunction
9, như trong ví dụ trên, hãy đảm bảo đường ống xây dựng của bạn không thay đổi tên chức năng hoặc không giả sử hàm có tên cụ thể.Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec-Chế độ-INSTANCES-NAME # sec-function-instances-name |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêm
- Một thuộc tính polyfill cho các chức năng '
6 có sẵn trongconst someFunction = function someFunctionName[] {}; someFunction.name; // "someFunctionName"
7const someFunction = function someFunctionName[] {}; someFunction.name; // "someFunctionName"
8function someFunction[] {} someFunction.name = 'otherFunction'; console.log[someFunction.name]; // someFunction