简体   繁体   中英

How to pass a prototype function to my object without needing to call it?

I am creating a script that registers products and already assigns each one unique code, this code is generated through a codeGenerator function.

I would like to know how I could pass this function as a prototype, without the need to call the function to assign my codes, more or less this way:

Vacuum = registerProduct('LG - Vacuum Cleaner 3000','appliances','Super smart vacuum cleaner sucking: water, dust and souls','300')

Returning an object:

APPLIANCES: Product {name: "LG - Vacuum Cleaner 3000", category: "appliances", description: "Super smart vacuum cleaner sucking: water, dust and souls", price: "R $: 300.00", code: "API- 01 "}

I get that same exit more this way:

 const productsRegistered = {} console.log(productsRegistered.length); const Product = function( name, category, description, price, code) { this.name = name this.category = category this.description = description this.price = 'R$: ' + price + '.00' this.code = codeGenerator(this.category) } function codeGenerator(productCategory) { let productCode = '' if (productCategory.length <= 3){ productCode = productCategory } else { productCode = sliceWords(productCategory) } if (typeof productList === 'undefined') { productCode += '-01' } else { productCode += '-0' + productList.length + 1 } return productCode } function registerProduct( name, category, description, price ) { productsRegistered[category.toUpperCase()] = new Product(name,category,description,price) } function sliceWords(word) { code = '' for(let n = 0; n <= 5; n += 2) { code += word[n].toUpperCase() } return code } Computer = registerProduct('HP - ALL IN ONE','Computers and Laptops','HP Computer Entel Core i10','1500') Vacuum = registerProduct('LG - Vacuum Cleaner 3000','appliances','Super smart vacuum cleaner sucking: water, dust and souls','300') console.log(productsRegistered);

All you really need to do is change the productsRegistered from an array to an object. If you have an array, the items aren't visible when you log them, because they're not on array index properties.

Because registerProduct doesn't return anything, you shouldn't assign the return value to a variable - instead, examine the productsRegistered object.

 const productsRegistered = {}; const Product = function( name, category, description, price, code) { this.name = name this.category = category this.description = description this.price = 'R$: ' + price + '.00' this.code = codeGenerator(productsRegistered) } function codeGenerator(productList) { let productCode = '' for (product in productList) { let productCategory = productList[product].category if (productCategory.length <= 3){ productCode = productCategory } else { productCode = sliceWords(productCategory) } if (productList.length === 0) { productCode += '-01' } else { productCode += '-0' + productList.length + 1 } } return productCode } function registerProduct( name, category, description, price ) { productsRegistered[category.toUpperCase()] = new Product(name,category,description,price) } function sliceWords(word) { code = '' for(let n = 0; n <= 5; n += 2) { code += word[n].toUpperCase() } return code } registerProduct('HP - ALL IN ONE','Computers and Laptops','HP Computer Entel Core i10','1500') registerProduct('LG - Vacuum Cleaner 3000','appliances','Super smart vacuum cleaner sucking: water, dust and souls','300') console.log(productsRegistered);

Or, if you do want the product to be visible in the productsRegistered object and in the return value of calling registerProduct , save the instantiated Product in a variable so you can both assign it to the object and return it:

 const productsRegistered = {}; const Product = function(name, category, description, price, code) { this.name = name this.category = category this.description = description this.price = 'R$: ' + price + '.00' this.code = codeGenerator(productsRegistered) } function codeGenerator(productList) { let productCode = '' for (product in productList) { let productCategory = productList[product].category if (productCategory.length <= 3) { productCode = productCategory } else { productCode = sliceWords(productCategory) } if (productList.length === 0) { productCode += '-01' } else { productCode += '-0' + productList.length + 1 } } return productCode } function registerProduct(name, category, description, price) { const product = new Product(name, category, description, price); productsRegistered[category.toUpperCase()] = product; return product; } function sliceWords(word) { code = '' for (let n = 0; n <= 5; n += 2) { code += word[n].toUpperCase() } return code } const computer = registerProduct('HP - ALL IN ONE', 'Computers and Laptops', 'HP Computer Entel Core i10', '1500') const vacuum = registerProduct('LG - Vacuum Cleaner 3000', 'appliances', 'Super smart vacuum cleaner sucking: water, dust and souls', '300') console.log(productsRegistered); console.log(computer);

If you want each value of the productsRegistered object to be an array instead of an object, then create the array at the property if it doesn't exist there yet, then push to it:

 const productsRegistered = {}; const Product = function(name, category, description, price, code) { this.name = name this.category = category this.description = description this.price = 'R$: ' + price + '.00' this.code = codeGenerator(this.category) } function codeGenerator(productCategory) { let productCode = '' if (productCategory.length <= 3) { productCode = productCategory } else { productCode = sliceWords(productCategory) } if (typeof productList === 'undefined') { productCode += '-01' } else { productCode += '-0' + productList.length + 1 } return productCode } function registerProduct(name, category, description, price) { const product = new Product(name, category, description, price); const key = category.toUpperCase(); if (!productsRegistered[key]) { productsRegistered[key] = []; } productsRegistered[key].push(product); return product; } function sliceWords(word) { code = '' for (let n = 0; n <= 5; n += 2) { code += word[n].toUpperCase() } return code } const computer = registerProduct('HP - ALL IN ONE', 'Computers and Laptops', 'HP Computer Entel Core i10', '1500') const vacuum = registerProduct('LG - Vacuum Cleaner 3000', 'appliances', 'Super smart vacuum cleaner sucking: water, dust and souls', '300') console.log(productsRegistered); console.log(computer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM