google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






10 thủ thuật JavaScript đơn giản dành cho người mới học Vâng, đúng như tiêu đề của bài viết đã nói, 10 thủ thuật lập trình này vô cùng nhỏ gọn và đơn giản để sử dụng nhưng hiếm khi chúng ta nhớ. Vì vậy tác giả liệt kê giúp bạn các thủ thuật để tối ưu và đơn giản hóa các công việc cần phải thực hiện khi lập trình với ngôn ngữ JavaScript.


Nhãn: 10, thủ thuật JavaScript, người mới học, thủ thuật lập trình, nhỏ gọn, đơn giản, tối ưu

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

Organize and Improve Functions with Objects as Parameters

A very common use of objects in modern web development is to use them as parameters of a function. It is always difficult to remember the order of the parameters of a function; however, using an object is very useful because then we do not have to be concerned about the order of the parameters. Moreover, it is more organized in order to understand what we are doing. This method allows for you to organize and improve the functions with objects as parameters. For example:

function insertData(name,lastName,phone,address){
  code here;
}

Could be remade like this:
function insertData(parameters){
  var name = parameters.name;
  var lastName = parameters.lastName;
  var phone = parameters.phone;
  var address = parameters.address;
}

It also is very useful at the time to have defaults values. Example:

function insertData(parameters){
  var name = parameters.name;
  var lastName = parameters.lastName;
  var phone = parameters.phone;
  var address = parameters.address;
  var status = parameters.status || 'single' //If status is not defined as a property in the object the variable status take single as value
}

To use the function now is pretty simple; we could send the data in two ways:

//Example 1
insertData({name:'Mike', lastName:'Rogers', phone:'555-555-5555',address:'the address', status:'married'});

//Example 2
var myData = {       name:'Mike', 
                         lastName:'Rogers', 
                               phone:'555-555-5555',
                             address:'the address', 
                                status:'married'
                       };

insertData(myData);

Functions are Data

Functions are data just like strings or numbers and we can pass them as functions parameters to create very amazing and commanding web applications. This method is very useful and is utilized by most all major frameworks. For example:

function byId(element, event, f){
  Document.getElementById(element).['on'+event] = f; //f is the function that we pass as parameter
}

byId('myBtn','click',function(){alert('Hello World')});

Another example of functions as data:

//Example 1
function msg(m){
  Alert(m);
}

//Example 2
var msg = function(m){ alert(m);}

Those functions are exactly the same. The only difference is how to use them for example: the first function you can use before it is declared; however, the second one should be declared in order to work:

//Example 1
msg('Hello world'); //This will work

function msg(m){
  alert(m);
}

//Example 2
msg('Hello world'); //Does not work because JavaScript cannot find the function msg because is used before is been declared.

var msg = function(m){ alert(m)}

Extend Native Objects

Even though some JavaScript gurus do not recommend this technique, it has been used by some frameworks. It allows you to create some helper methods, to the JavaScript API.

//We create the method prototype for our arrays
//It only sums numeric elements

Array.prototype.sum = function(){
    var len = this.length;
    total = 0;
    for(var i=0;i<len;i++){
        if(typeof this[i]!= 'number') continue;
         total += this[i];
    }
    return total;
}

var myArray = [1,2,3,'hola'];
myArray.sum();

Array.prototype.max = function(){
    return Math.max.apply('',this);
}

Boolean

Be aware of checking these comparisons, because it will save you time from debugging the script.

'' == '0'          // false
0 == ''            // true
0 == '0'           // true
false == 'false'   // false
false == '0'       // true
false == undefined // false
false == null      // false
null == undefined  // true
true == 1          // true
'' == null         // false
false == ''        // true
iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web