Home > Web Front-end > JS Tutorial > 'Three Steps' to Using Reference Type Objects

'Three Steps' to Using Reference Type Objects

巴扎黑
Release: 2016-11-25 10:25:11
Original
1241 people have browsed it

Reference type:

The reference type is usually called a class, that is to say, when a reference value is encountered, the object is processed.

JavaScript’s built-in objects - String, Array, Number, and Object are all reference types. The objects created by us users are actually instances of these types. In the JavaScript object-oriented programming process, reference types are used to support 'classes'. The corresponding logic appears.

            Reference types are composed of three parts: constructor, method definition and properties.方法 Take the POS machine system of the store settlement cashier in the object -oriented method as an example. Let's understand the "three steps" of the reference type:

The first step is to define the reference type; :

Js code

function Cart(inputs){

this.inputs=inputs;

this.item_info=loadAllItems();

}/*inputs is the product information passed into the POS system for customers to purchase goods. loadAllItems() is to call the

information of all the products in the local product library (it is only used to illustrate the problem and does not need to be delved into)*/

​ The this keyword is used here. In the constructor or method, the this keyword represents the current value of the reference type. Object instance,

Java code

this.inputs=inputs;

This line of code refers to the inputs attribute of the current object instance and sets it as the parameter inputs (the barcode of the product information passed into the POS machine system for the customer to purchase the product) barcode).

The second step is to create the attribute method;

Js code

Cart.prototype.get_item_base_info=function(normal_barcode){

return _.filter(this.item_info,function(item){

         return normal_barcode.indexOf( item.barcode)>=0;

})

}//Get information about the product corresponding to the barcode

Cart.prototype.add_item_count=function(normal_barcode,item_base_info) { var counts = _.values(_ .countBy(normal_barcode));

_.each(item_base_info,function(item){

item.count=counts[_.indexOf(item_base_info,item)];

})

return item_base_info

}/ /Remove the same barcode and record the quantity of the product

The third step is to create an instance of the reference type;

Js code

var cart=new Cart(inputs);//Create an instance cart

It should be noted here that the new key is used Word, code written without using the new keyword will not work properly, and methods in the constructor and its prototype's properties will not be executed until instantiated. After instantiation, you can call the prototype method and input data in the constructor.

Js code

var item_base_info=cart.get_item_base_info(normal_barcodes);/*Call the instance method to obtain the product information corresponding to the barcode

; normal_barcode is the processed input barcode that can be recognized by the system (that is, the product purchased by the customer Recognizable bar code

);*/

var item_count_add=cart.add_item_count(normal_barcodes,item_base_info);/*Call the instance method to remove the same bar code

and record the quantity of the product;*/

var shopping_list=print_shoppinglist(item_count_add);/*print_shoppinglist is the method of multiplying the unit price of the product by the quantity. */

Console.log(shopping_list)//Output and print the results

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template