javascript-objects

Difference between Class(), new Class and new Class()

元气小坏坏 提交于 2019-12-23 12:11:12
问题 What are the differences between Class() and new Class , new Class() ? I did a test and the later seems to do be quicker. http://jsperf.com/object-initilzation I read there is no difference, but there appears to be. 回答1: Class() Calls a function. Don't use this on constructor functions. new Class and new Class() There is no difference between these, both instantiate a new instance of a class. The parens are optional if there are no arguments being passed and the new keyword is used. 回答2:

Trying to understand the new keyword

大城市里の小女人 提交于 2019-12-22 17:46:48
问题 I'm trying to understand exactly what the javascript new keyword means, why it is necessary and when I should use it. Consider the following examples: var x = new function(){ var self=this; this.myFunction = function(){alert('foo ' + self.v)} this.v='x'; }; var y = function(){ var self=this; this.myFunction = function(){alert('foo ' + self.v)} this.v='y'; return this; }(); var f=function(){ var self=this; this.myFunction = function(){alert('foo ' + self.v)} this.v='z'; } var z = new f(); x

Convert array values to object keys

旧巷老猫 提交于 2019-12-22 04:27:06
问题 I do a get which returns me a json object like so: "data": [ [ "2016 Pass/Fail Rates by Test Centre", "", "", "", "", "", "", "", "", "" ], [ "", "Passes", "", "No ID", "", "Fails", "", "Fail Dangerous", "", "Total" ], [ "Sometown", "8,725", "53.40%", "140", "0.90%", "7,417", "45.40%", "48", "0.30%", "16,330" ], [ "Some Other Town", "12,778", "44.80%", "193", "0.70%", "15,422", "54.10%", "103", "0.40%", "28,496" ], [... many more identically formatted arrays ...] and I would like to end up

Removing an argument from arguments in JavaScript

*爱你&永不变心* 提交于 2019-12-22 04:09:32
问题 I wanted to have an optional boolean parameter to a function call: function test() { if (typeof(arguments[0]) === 'boolean') { // do some stuff } // rest of function } I want the rest of the function to only see the arguments array without the optional boolean parameter. First thing I realized is the arguments array isn't an array! It seems to be a standard Object with properties of 0, 1, 2, etc. So I couldn't do: function test() { if (typeof(arguments[0]) === 'boolean') { var

Reduce array to object using arrow function

断了今生、忘了曾经 提交于 2019-12-21 06:56:08
问题 I'm playing around with the limits of array and arrow functions and I'm trying to convert this reduce function into an arrow function: var monthsById = months.reduce(function(result, month) { result[month.Id] = month; return result; }, {}); But I'm having trouble to return the map, since result[month.Id] = month; will return the month and not the map like in this approach: var monthsById = months.reduce((byId, month) => byId[month.Id] = month, {}); So I'm looking for a single statement, that

How do I get current rowindex of a table using Javascript?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 07:48:03
问题 Can I get current row index of a table in Javascript and can we remove the row of table with current Index that we got? 回答1: The rowIndex property returns the position of a row in table function myFunction(x) { console.log("Row index is: " + x.rowIndex); } <table> <tr onclick="myFunction(this)"> <td>Click to show rowIndex</td> </tr> <tr onclick="myFunction(this)"> <td>Click to show rowIndex</td> </tr> <tr onclick="myFunction(this)"> <td>Click to show rowIndex</td> </tr> <tr onclick=

Duplicate of Product in Shopping Cart

偶尔善良 提交于 2019-12-20 05:39:11
问题 I have a simple problem in my shopping cart function. After i clicked the add to cart button, if it has the same product ID, it outputs a new product in a new row. It should just increase the product's quantity if it has the same product ID. const products = []; const carts = []; const inputs = { id: document.getElementById("productID"), desc: document.getElementById("product_desc"), qty: document.getElementById("quantity"), price: document.getElementById("price") }; const productsTable =

Ngrx : Cannot assign to read only property 'Property' of object '[Object]'

我的未来我决定 提交于 2019-12-20 02:48:06
问题 I'm using ngrx store. In my state I have to items export interface ISchedulesState { schedulings: ISchedules; actualTrips: ISchedule[]; } Here are my interfaces export interface ISchedules { [key: string]: ISchedule[]; } export interface ISchedule { dest: number; data: string } In reducer I update actualTrips export const SchedulingReducers = ( state = initialSchedulingState, action: SchedulesAction ): ISchedulesState => { switch (action.type) { case ESchedulesActions.GetSchedulesByDate: {

Why does jQuery Extend Deep Copy not recursively copy an object?

[亡魂溺海] 提交于 2019-12-19 17:27:44
问题 I've searched everywhere and found similar questions with answers that didn't really address my issue so I apologize if this seems like a repeat, but it appears from my experimenting that jQuery's deep copy function doesn't actually work as it's described (or maybe I'm misreading its description). Here's an example demonstrating the problem I'm having: http://jsfiddle.net/wcYsH/ Or this for download: https://github.com/kevroy314/jQuery-Extend-Test Why does the data in the previous copy get

Getting value from object in javascript

痞子三分冷 提交于 2019-12-19 05:06:39
问题 After console.log(item) I got following object. Object {_value: "106", _id: "opt_zXtP8TOfW2zteOgDIJQDI6Bxx3xSTkl5", _class: "selected", _selected: "selected"} I want _value from this object. How can I do this? I tried alert(item.value) but gets undefined 回答1: You're ignoring the underscore. value and _value are not the same. You need to use: item["_value"]; Or: item._value; 来源: https://stackoverflow.com/questions/21305400/getting-value-from-object-in-javascript