[英]How do you implement something like a static method in JavaScript?
While I was looking through the documentation for String
, referencing String.fromCharCode
. 在浏览String
文档时,引用了String.fromCharCode
。 I noticed that it was different from the majority of the other String
methods in the way that it didn't have a .prototype
before the method, Ex. 我注意到它与大多数其他String
方法的不同之处在于,在方法Ex之前没有.prototype
。 String.prototype.CharCodeAt
. String.prototype.CharCodeAt
。 I assume that it must be similar to that of a static reference in java given that it's usage requires you to reference the String
class/Object, Ex. 我假定它必须类似于java中的静态引用,因为它的用法要求您引用String
类/对象,例如。
var str = "foo";
var code = str.charCodeAt(0);
var character = String.fromCharCode(code); // This is the reference I'm talking about
alert(code); // Output: 102
alert(character); // Output: f
My question is how would you implement a method so that it is accessed in the same style as String.fromCharCode
in your own code? 我的问题是,您将如何实现一个方法,以便在您自己的代码中以与String.fromCharCode
相同的样式进行访问?
In JavaScript, functions are first-class objects. 在JavaScript中,函数是一流的对象。 It makes it possible to assign method property directly to it, which would behave as static method: 这样就可以直接为其分配方法属性,该属性的行为类似于静态方法:
function MyObject() {}
MyObject.static = function() { ... }
Just like you would expect. 就像您期望的那样。
DZone's ref card provides a great reference for the Object-oriented javascript, I highly recommend it. DZone的 ref卡为面向对象的javascript提供了很好的参考,我强烈推荐它。 The related paragraphs 相关段落
There is no direct support for static members. 没有直接支持静态成员。 Constructor functions are used to create static members. 构造函数用于创建静态成员。 Static members can't be accessed with “this” keyword. 静态成员无法使用“ this”关键字进行访问。
private static members 私人静态成员
function Factory(){}
// public static method
Factory.getType = function (){
return “Object Factory”;
};
// public static field
Factory.versionId = “F2.0”;
Factory.prototype.test = function(){
console.log(this.versionId); // undefined
console.log(Factory.versionId); // F2.0
console.log(Factory.getType()); // Object Factory
}
var factory = new Factory();
factory.test();
public static members 公共静态成员
var Book = (function () {
// private static field
var numOfBooks = 0;
// private static method
function checkIsbn(isbn) {
if (isbn.length != 10 && isbn.length != 13)
throw new Error(“isbn is not valid!”);
}
function Book(isbn, title) {
checkIsbn(isbn);
this.isbn = isbn;
this.title = title;
numOfBooks++;
this.getNumOfBooks = function () {
return numOfBooks;
}
}
return Book;
})();
var firstBook = new Book(“0-943396-04-2”, “First Title”);
console.log(firstBook.title); // First Title
console.log(firstBook.getNumOfBooks()); // 1
var secondBook = new Book(“0-85131-041-9”, “Second Title”);
console.log(firstBook.title); // First Title
console.log(secondBook.title); // Second Title
console.log(firstBook.getNumOfBooks()); // 2
console.log(secondBook.getNumOfBooks()); // 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.