javascript-objects

JavaScript variable inside class is always undefined

风格不统一 提交于 2019-12-25 14:06:51
问题 I have a JavaScript class like this: Dog = (function() { var name; function setName(_name) { name = _name; } return { setName: setName, name: name }; })(); When I run: Dog.setName('Hero'); Dog.name is always undefined. I am certainly missing something about JS scoping, but what? 回答1: You are returning an object where name property has a value of name at that point in time (which is undefined). The name property of the returned object is not somehow dynamically updated when the name variable

Generate Array of Objects of different Objects Javascript

喜你入骨 提交于 2019-12-25 09:27:54
问题 I have severel Objects containing one sort of data: Prices: 'btc-usd' : 2640, 'ltc-usd': 40, ... Amount of Crypto: 'btc-usd': 2.533, 'ltc-usd': 10.42, ... How can I take these Objects and create an Array of Objects like: [ { name: 'Bitcoin', amount: 2.533, value: 2640, id: 'btc-usd' }, { name: 'Litecoin', amount: 10.42, value: 40, id: 'ltc-usd' }, ... ] Thanks a lot for your help! 回答1: You could map the keys of one of the objects to produce a new array of objects. You just have to make sure,

JavaScript OOP: Objects change their prototype (for all other objects using the same prototype) [duplicate]

吃可爱长大的小学妹 提交于 2019-12-25 05:34:25
问题 This question already has answers here : What does “the prototype belongs to the class not the instance” mean in javascript? (3 answers) Closed 5 years ago . function NamedRoundedBlock(){ var name = this.makeFeild("name"); name.className = "Block NamedRound name"; this.element.className = "Block NamedRound root"; this.element.appendChild(name); } NamedRoundedBlock.prototype = new Block(); NamedRoundedBlock.prototype.constructor = NamedRoundedBlock; There's the code, anyone know what I've done

JS objects attached to HTML elements

青春壹個敷衍的年華 提交于 2019-12-25 04:31:30
问题 I've been getting more into oop with js (I'm classically trained in Java) and I have four boxes. I want to do something like this: <div class="box"><\div> <div class="box"><\div> <div class="box"><\div> <div class="box"><\div> Function Box(){ //PRIVATE VAR var count=0; } Box.prototype.move = function(){ this.css({left:50}); } Box.prototype.click= function(){ this.count++; } //create new instances of box and assign or connect to the four HTML elements For (var i = 0; i < $('.box').length; i++)

JS objects attached to HTML elements

℡╲_俬逩灬. 提交于 2019-12-25 04:31:16
问题 I've been getting more into oop with js (I'm classically trained in Java) and I have four boxes. I want to do something like this: <div class="box"><\div> <div class="box"><\div> <div class="box"><\div> <div class="box"><\div> Function Box(){ //PRIVATE VAR var count=0; } Box.prototype.move = function(){ this.css({left:50}); } Box.prototype.click= function(){ this.count++; } //create new instances of box and assign or connect to the four HTML elements For (var i = 0; i < $('.box').length; i++)

Javascript. Does it exists depth limit when object has reference to itself ? How does interpreter optimizes this case?

二次信任 提交于 2019-12-25 04:26:37
问题 If I understand correctly, in code below exists recursive reference: var object = {} object.nested = object; console.log(object) In console I can see infinite reference. So, how does interpreter optimizes this case ? How does this reference works ? Does it cause the memory leaks ? Does it exists depth limit ? Plunker: http://plnkr.co/edit/QbR3o8cGnXU924nNs0cE 来源: https://stackoverflow.com/questions/40452539/javascript-does-it-exists-depth-limit-when-object-has-reference-to-itself-how

Need help parsing imported json data with VUE.JS JavaScript

戏子无情 提交于 2019-12-25 03:12:07
问题 I need to import JSON data and pull out specific things from it and am having trouble doing it. This is the sample data: { "0bEiO5zcBCHv3Wd2lxHjjRepB":{ "name":"math", "credits":4, "professors":[ "Samatha" ], "description":"come and learn some math", "prereqs":"", "comment":"", "maxEnrollment":100, "times":[ { "day":2, "start":900, "end":1100 }, { "day":4, "start":900, "end":1100 } ], "departments":[ "mathematics" ], "submitted":true }, "BsSbrbjTH5FyV7gWdPjeDPqpw":{ "name":"biology", "credits

Delete null values in nested javascript objects

a 夏天 提交于 2019-12-24 19:43:11
问题 I have a nested object and want to remove all key/value pairs if the value is null or undefined. I've managed to get the below code working but it doesn't check the nested key/value pairs and wondered if someone could help me figure out what needs adding to the code please? var myObj = { fName:'john', lName:'doe', dob:{ displayValue: null, value: null }, bbb:null }; function clean(obj) { for (var propName in obj) { if (obj[propName] === null || obj[propName] === undefined || obj[propName] ===

Javascript - use string as object reference

你。 提交于 2019-12-24 15:11:06
问题 If I have a bunch of objects, and within these objects is the string "id" (which is the same as the object name) how can I use this string to reference the object? Example: //These objects are tests - note the id's are the same as the object name var test1 = { id : "test1", data : "Test 1 test 1 test 1" } var test2 = { id : "test2", data : "Test 2 test 2 test 2" } /* ----- My Function ----- */ var myObj = null; function setMyObj(obj){ myObj = obj; } setMyObj(test1); /* ----- My Function -----

setting unset properties as default properties in a function?

ぃ、小莉子 提交于 2019-12-24 14:39:33
问题 I'm pretty new to JS and i'm trying to learn some basics in functions. I'm facing a problem, I've created a function like this: function someName (element, settings={i:"#1d252c", i2:"#fff"}) { ....... } If i call the function as someName(element) . i and i2 retain their values, however If i call it like someName(element,{i:"#3acbda"}) , the value of i does change however the value of i2 is undefined, So how do i assign the default value to a property when the settings object is no longer the