简体   繁体   English

您如何在JavaScript中实现类似静态方法的功能?

[英]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.

相关问题 如何在javascript中实现类似指针的功能? - How do I implement something like pointers in javascript? 您如何在Ember.js中的javascript中复制诸如function(){…} .property()之类的内容? - How do you replicate something like function(){ … }.property() in javascript like in Ember.js? 在jquery中有像indexof()这样的方法吗? 或者怎么做? - Is there something method like indexof() in jquery? Or how to do that? Javascript:创建对象时,如何设置myObject.name之类的内容? - Javascript: when creating an object, how do you set something like myObject.name? 你如何扫描javascript中是否有活动的东西 - how do you scan if there is something active in javascript 你如何在 ReactJS 中限制静态方法? - How do you throttle a static method in ReactJS? 你如何在JavaScript中实现一个保护条款? - How do you implement a guard clause in JavaScript? 你如何在 JavaScript 中实现 Stack 和 Queue? - How do you implement a Stack and a Queue in JavaScript? 使用 static 方法实现一个列表 javascript - implement a list with a static method javascript 如何避免每次执行排序、过滤等操作时都发出 fetch() 请求。香草 JavaScript 中的一些 JSON 数据? - How to avoid making a fetch() request every time you do something like sort, filter, etc. some JSON data in vanilla JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM