Javascript 为什么使用函数来创建数据结构而不是类?这是正确的吗?

Javascript 为什么使用函数来创建数据结构而不是类?这是正确的吗?,javascript,data-structures,Javascript,Data Structures,我比较了我在网上找到的代码和我的讲师给我的代码。我很困惑,为什么我的讲师使用函数来创建数据结构,而不是像我在网上发现的那样创建类。哪一个更好?上课?功能 这是我的讲师编写的创建堆栈的代码 var Stack = function(){ //Class members this.count = 0; this.storage = {}; //Add item this.push = function(value){ this.storage

我比较了我在网上找到的代码和我的讲师给我的代码。我很困惑,为什么我的讲师使用函数来创建数据结构,而不是像我在网上发现的那样创建类。哪一个更好?上课?功能

这是我的讲师编写的创建堆栈的代码

var Stack = function(){
    //Class members
    this.count = 0;
    this.storage = {};

    //Add item
    this.push = function(value){
        this.storage[this.count] = value;
        this.count++;
    }
    //Delete item
    this.pop = function(){
        if(this.count === 0){
            return undefined;
        }
        this.count--;
        var result = this.storage[this.count];
        delete this.storage[this.count];
        return result;
    }

    //Return the sie of the stack
    this.size = function(){
        return this.count;
    }

    //View the top of the stack
    this.top = function(){
        return this.storage[this.count-1];
    }
}
这是我的讲师创建链接列表的代码

function Queue(){
    this.collection = [];

    //Print the collection
    this.print = function(){
        document.write(this.collection + "<br/>");
    };

    //Add item in queue
    this.addQ = function(element){
        this.collection.push(element);
    };

    //Remove item at the front
    this.deQ = function(){
        return this.collection.shift(); //Left shift
    };

    //Return first item
    this.front = function(){
        return this.collection[0];
    };

    //Return the size of queue
    this.size = function(){
        return this.collection.length;
    };

    //Check the queue status: Empty or not
    this.isEmpty = function(){
        return (this.collection.length === 0);
    };
}
函数队列(){
this.collection=[];
//打印收藏
this.print=函数(){
document.write(this.collection+“
”); }; //在队列中添加项目 this.addQ=函数(元素){ 这个.collection.push(元素); }; //移除前面的项目 this.deQ=函数(){ 返回此.collection.shift();//左移位 }; //返回第一项 this.front=函数(){ 返回此.集合[0]; }; //返回队列的大小 this.size=函数(){ 返回此.collection.length; }; //检查队列状态:是否为空 this.isEmpty=函数(){ 返回(this.collection.length==0); }; }
就几个问题

  • 为什么使用函数而不是类

  • 为什么对堆栈使用
    var Stack=function(){
    ,对队列使用
    function Queue(){
    ?有什么不同吗

  • 为什么要使用
    this.push=function(value){
    ?我认为它应该是类似
    function push()的函数{


  • 首先,使用的两种方法没有区别,它们的工作方式完全相同。
    另外,
    this.push=function(value){…}
    是如何在Javascript中定义方法的

    首先,使用的两种方法没有区别,它们的工作方式完全相同。
    另外,
    this.push=function(value){…}
    是如何在Javascript中定义方法的Javascript是一种基于原型的语言,因此它没有真正的类。然而,ES6添加了
    关键字,这更容易阅读,但仍然在幕后使用原型

    javascript中的所有内容都是一个对象,包括函数。当您使用
    new
    关键字调用函数时,它会创建一个新对象并将其附加到该函数中的
    this
    。因此,在构造函数中,您可以执行
    this.foo=function(){
    将函数附加到新对象。但是,这是低效的,因为每次创建新对象时它都会运行。更好的方法是使用原型,这样每个函数只创建一次:

    var Stack = function() {
        // Initialize local variables.
        this.count = 0;
        this.storage = {};
    }
    
    // Attach class methods
    Stack.prototype.foo = function() { ... }
    Stack.prototype.bar = function() { ... }
    
    // Create instance of class.
    var myStack = new Stack();
    

    由于最近添加了
    关键字,如果您想支持IE11或其他较旧的浏览器,就需要使用函数式方式,除非您使用的是transpiler。

    Javascript是一种基于原型的语言,因此它没有真正的类。然而,ES6添加了
    关键字,这更易于阅读,但是ll在引擎盖下使用原型

    javascript中的所有内容都是一个对象,包括函数。当您使用
    new
    关键字调用函数时,它会创建一个新对象并将其附加到该函数中的
    this
    。因此,在构造函数中,您可以执行
    this.foo=function(){
    将函数附加到新对象。但是,这是低效的,因为每次创建新对象时它都会运行。更好的方法是使用原型,这样每个函数只创建一次:

    var Stack = function() {
        // Initialize local variables.
        this.count = 0;
        this.storage = {};
    }
    
    // Attach class methods
    Stack.prototype.foo = function() { ... }
    Stack.prototype.bar = function() { ... }
    
    // Create instance of class.
    var myStack = new Stack();
    

    由于最近添加了
    class
    关键字,如果您想支持IE11或其他较旧的浏览器,就需要使用函数式方式,除非您使用的是transpiler。

    嘿,您知道,在真实的javascript中,您可能永远不会编写自己的堆栈或队列类,因为内置数组已经像sta一样工作了ck和队列。您的讲师只是将它们用作示例。嘿,正如您所知,在真实的javascript中,您可能永远不会编写自己的堆栈或队列类,因为内置数组已经像堆栈和队列一样工作。您的讲师只是将它们用作示例。