Javascript jQuery:我们如何实现if…else逻辑和调用函数

Javascript jQuery:我们如何实现if…else逻辑和调用函数,javascript,jquery,Javascript,Jquery,我是jQuery新手,所以如果这个问题听起来很愚蠢,请不要介意,但我正在尝试做以下几件事: 我有3个功能,如: AddToCart Function which adds item to the shopping cart: //offer_id is the offer which we are trying to add to cart. addToCart: function(offer_id) { }, RemoveFromCart which remove

我是jQuery新手,所以如果这个问题听起来很愚蠢,请不要介意,但我正在尝试做以下几件事:

我有3个功能,如:

AddToCart Function which adds item to the shopping cart:
//offer_id is the offer which we are trying to add to cart. 
    addToCart: function(offer_id)
    {
    },


RemoveFromCart which removes data from the cart
//target is link clicked and event is the click event. 
    removeFromCart: function(target, event)
    {
    },

Get the current state of the cart   
//return string which represents current state of cart. 
    getCartItems: function()
    {
    }
现在我试着做三件事:

  • 如果购物车中没有
    内容
    调用
    addToCart
    要比调用
    some action
    好,所以这里基本上我们需要检查购物车的当前状态,这是通过调用
    getcarttItems
    获得的,如果它是
    Null
    并且如果调用
    addToCart
    要比执行
    一些操作好得多
  • 如果购物车中有
    内容
    调用
    addToCart
    比调用
    some action
    ,因此,基本上这里我们需要检查购物车的当前状态,这是通过调用
    getCartItems
    获得的,并检查它是否为
    Null
    ,如果调用
    addToCart
    ,则如果购物车中有一些内容,则执行
    一些操作
    
  • 如果购物车中有
    内容
    removeFromCart
    被称为
    some action
    ,因此基本上这里我们需要检查cart的当前状态,这是通过调用
    getCartItems
    获得的,如果它不为空,并且如果调用了
    removeFromCart
    ,那么我们将执行
    一些操作
  • 我正在尝试执行的伪代码:

        if there is no content in cart 
            and addToCart is called than 
            $(document).track(
                );
    
        if there is content in the cart 
            and addToCart is called than
            $(document).track(
                );
    
        if there is content in the cart 
            and removeFromCart is called 
            $(document).track(
                );
    

    我最关心的是,我是jQuery和JavaScript的新手,因此不确定如何实现if…else逻辑,以及如何使用jQuery/JavaScript调用函数

    它类似于任何其他编程语言

    if() {
        ..
    }
    else if() {
        ..
    }
    else if() {
        ..
    }
    
    但是,由于这些操作必须在调用
    addToCart
    removeFromCart
    时执行,因此一个简单的解决方案是将这些条件放入函数本身:

    addToCart: function(offer_id) {
        // add to cart is called and cart IS empty
        if(this.getCartItems().length == 0) {
    
        }
        // add to cart is called and cart is NOT empty
        else {
    
        }
    }
    
    removeFromCart: function(target, event) {
        // remove was called and cart has items
        if(this.getCartItems().length > 0) {
    
        }
    }
    

    它类似于任何其他编程语言

    if() {
        ..
    }
    else if() {
        ..
    }
    else if() {
        ..
    }
    
    但是,由于这些操作必须在调用
    addToCart
    removeFromCart
    时执行,因此一个简单的解决方案是将这些条件放入函数本身:

    addToCart: function(offer_id) {
        // add to cart is called and cart IS empty
        if(this.getCartItems().length == 0) {
    
        }
        // add to cart is called and cart is NOT empty
        else {
    
        }
    }
    
    removeFromCart: function(target, event) {
        // remove was called and cart has items
        if(this.getCartItems().length > 0) {
    
        }
    }
    

    那么,您只是在问如何“实现if…else逻辑以及如何使用jQuery/JavaScript调用函数”,还是为您编写代码?if(…){}else if(…){}else{}?如果逻辑是JS的而不是Jquery的。它在Jquery中,因此不确定如何实现它。如果购物车中已经有内容,那又有什么关系呢?如果没有内容,则将项目添加到购物车,现在有一个项目。如果购物车中有4个项目,请将项目添加到购物车,现在有5个项目。不管以前的状态如何,都要做同样的事情。我认为你的问题在于你的实际逻辑。与其每次都重新计算购物车,不如使用一个变量:
    myCart
    ,它是一个基本的JS对象,并在开始时创建它(空),然后添加和删除购物车,而不必担心它是否存在或有内容。@Anthony:我理解,但从Omniture方面生成报告,我们有这个要求,我同意这是没有意义的,但Omniture会基于这个输入生成更好的报告。所以,您只是问如何“实现if…else逻辑,如何使用jQuery/JavaScript调用函数”,还是为您编写代码?if(…){}else if(…){}else{else}?如果逻辑是JS的而不是Jquery的。它在Jquery中,因此不确定如何实现它。如果购物车中已经有内容,那又有什么关系呢?如果没有内容,则将项目添加到购物车,现在有一个项目。如果购物车中有4个项目,请将项目添加到购物车,现在有5个项目。不管以前的状态如何,都要做同样的事情。我认为你的问题在于你的实际逻辑。与其每次都重新计算购物车,不如使用一个变量:
    myCart
    ,它是一个基本的JS对象,并在开始时创建它(空),然后添加和删除购物车,而不必担心它是否存在或有内容。@Anthony:我理解,但从Omniture方面生成报告,我们有这个要求,我同意这是没有意义的,但Omniture会根据这些输入生成更好的报告。