jQuery in action notes.

本文介绍了JavaScript中的对象基础知识,包括函数作为一等公民的概念、对象实例的用途、属性的动态添加及访问方式。同时,对比了window.onload与jQuery的$(document).ready()方法的区别,并概述了如何通过扩展jQuery来增强其功能。

 

jQuery in action notes.

-------------------------------------
ch0. JavaScript Object fundamentals

functions are first-class objects in JavaScript

the primary purpose of an Object instance is to serve as a container for a named collection of other objects. like Map or dictionary in Java.

var shinyAndNew = new Object();
shinyAndNew.year = 2005;
need to add properties, dynamically as needed.
property access can be 2 forms:
dot operator: bith = shinyAndNew.year;
square op: birth = shinyAndNew["year"]; // shinyAndNew['year']

object literal
var shinyAndNew = {
     year: 2005,
     owner: {
          name: 'Spike Spiegel',
          occupation: 'bounty hunter'
     }
};

var shinyAndNew = {};
the same as
window.shinyAndNew = {};

an overview of the JavaScript Object:
A JavaScript object is an unordered collection of properties.
Properties consist of a name and a value.
Objects can be declared using object literals.
Top-level variables are properties of window.

Functions in JavaScript are considered objects like any of the other object types that are defined in JavaScript, such as Strings, Numbers, or Dates.
Function object can be: so it is the first-class object.
Assigned to variables
Assigned as a property of an object
Passed as a parameter
Returned as a function result
Created using literals

When we declare a top-level named function, a Function instance is created and assigned to a property of window.
the following statements are all equivalent:
function hello(){ alert('Hi there!'); }
hello = function(){ alert('Hi there!'); }
window.hello = function(){ alert('Hi there!'); }

passing functions as parameters, Functions as callbacks

the object referenced by keyword "this" — termed the function context — is determined not by how the function is declared but by how it’s invoked.

call() or apply()
We can set the function context to whatever we want by invoking a function via the Function methods call() or apply().
A function f acts as a method of object o when o serves as the function context of the invocation of f.

Closures

$(function(){
     var local = 1;
     window.setInterval(function(){
          $('#now').html('#' + local + ', ' + new Date());
          local++;
     },1000);
});
Although it is true that the block in which local is declared goes out of scope when the ready handler exits, the closure created by the declaration of the function, which includes local, stays in scope for the lifetime of the function.

Another important feature of closures is that a [function context](this object) is never included as part of the closure. but can use [outer object] to access.

a function declaration and its environment form a closure allowing the function, when later invoked, to access those local variables that become part of the closure.


-------------------------------------
ch1 Introducing jQuery

Unobtrusive JavaScript: segregate style(css), behavior(js) from structure within an HTML document.

window.onload = function() { /* do something */ };
basically equivalent to
jQuery(document).ready(function() { /* do something */ });
the same as

$(function() { /* do something */ });

but 1st one is on all resources loaded, while 2nd one only after DOM tree has loaded

In contrast, the window’s onload technique allows for only a single function.

Extending jQuery
$.fn.yourExtendedFunctionName = function() {
     //return the original set
}
AI Overview Implementing selective checkout with checkboxes on a cart page typically involves allowing users to select specific items in their cart for purchase, rather than requiring them to purchase all items. This functionality is not a standard feature in most e-commerce platforms and usually requires custom development or the use of specialized plugins/apps. General Approach: Modify the Cart Template: Add a checkbox next to each item in the cart. Ensure the checkbox is linked to the specific product or line item. Handle User Selections: Use JavaScript to detect changes in checkbox states (checked/unchecked). When a checkbox is selected or deselected, update the cart's subtotal and potentially the items that will be included in the checkout process. Adjust the Checkout Process: When the user proceeds to checkout, ensure that only the items corresponding to the selected checkboxes are passed to the order. This may involve modifying the platform's core checkout logic or using hooks/filters provided by the platform. Platform-Specific Considerations: WooCommerce (WordPress): Requires custom code in functions.php or a custom plugin to add checkboxes, manage selections, and modify the checkout process. You might use AJAX to update the cart dynamically as selections are made. Shopify: Involves modifying theme files (e.g., cart-template.liquid, theme.js) to add checkboxes and JavaScript to handle the logic. This often requires familiarity with Liquid (Shopify's templating language) and JavaScript. Other Platforms: The specific implementation will vary based on the platform's architecture and extensibility options. It may involve similar approaches of template modification and custom code. Important Notes: Complexity: Implementing selective checkout can be complex and may require advanced coding skills. User Experience: Carefully consider the user experience implications of selective checkout to ensure it is intuitive and does not cause confusion. Testing: Thoroughly test the functionality to ensure that selected items are correctly processed and that no loopholes exist (e.g., refreshing the page causing issues with selected items). I want to achieve this selective checkbox function in my cart page. Im using woocommerce and woodmart theme. Please advice on what to do
07-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值