javascript-objects

What JavaScript concept allows for the same name to be both a function and an object?

£可爱£侵袭症+ 提交于 2019-12-06 15:05:44
I've been using JavaScript for a few years now in web design/development, and everything I know has been self-taught (I was a design major and hobbyist front-ender turned full web developer in pursuit of my career). With that background, I discovered something that I want to learn more about and have no idea what it is called, how it works, or that it may even be something extremely simple. I've tried to search for more information about this (to prevent myself from needing to ask this), but it's difficult when I'm not sure what it's called that I'm even looking for... I noticed in a few

How to get keys (path to) the deepest nested object in a javascript nested object

时光毁灭记忆、已成空白 提交于 2019-12-06 13:40:56
问题 I have a nested javascript object like this: { "apple": { "orange": { "chilli": {}, "pineapple": { "mango": {} } }, "carrot": { "cabbage": {}, "onion": {} } } } i want to get the path (keys) of the deepest nested object. something like apple.orange.pineapple.mango any help is appriciated :) 回答1: You could return an array of arrays with the longest pathes. This works for more than one path with the same length. function getDeepest(object) { return object && typeof object === 'object' ? Object

Why does a method using the shorthand method syntax not contain a prototype object

孤街醉人 提交于 2019-12-06 13:15:13
In the code snippet below func2 is supposed to be the shorthand method syntax for func1 . Question 1 : Why does obj1 contain the prototype Object and obj2 does not (while both have the __proto__ object)? Question 2 : Are all three objects prototype objects? Question 3 : Why does the fact of obj2 not having a prototype function not influence the way in which it binds this ? Concerning obj3 : obj3 is there for reference because it is equivalent to obj2 in terms of not having a prototype function. It just binds this differently (In obj1 and obj1 this is determined "by the invocation, but not by

Rendering JavaScript Object in React

我的梦境 提交于 2019-12-06 12:29:08
I have a React project set up to fetch an Object from a Firebase database and render this to my page. However I do not know how to render the data properly. The data I am fetching looks like this: { "projects": { "0": { "title": "test", "function": "test2" }, "1": { "title": "test3", "function": "test4" } } } In the Chrome React Debugger I see this: <div> <Message key="0" data={function: "test2", title: "test", key: "0"}>...</Message> <Message key="1" data={function: "test4", title: "test3", key: "1"}>...</Message> </div> But in the elements view I am simply seeing two empty divs: <div> <div><

Is “this” necessary in javascript apart from variable definition

﹥>﹥吖頭↗ 提交于 2019-12-06 12:13:19
问题 My question is dead simple. I just casually discovered that once you have defined a property with this. into an object, you don't need to prepend this. anymore when you want to call them. So this. is really meant to be used ad definition time, like var ? I found it my self shortly after, i was referencing the window object with this. since i called my object without using new, so like it was a function. One extra question, maybe for comments. Inside the main object, if i create a new object,

Trying to understand the new keyword

非 Y 不嫁゛ 提交于 2019-12-06 05:35:42
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.myFunction(); y.myFunction(); z.myFunction(); x,y and z are all objects. Potentially with public and

From which version, IE can support Object.create(null)?

北城余情 提交于 2019-12-06 02:43:48
You can create an object in JavaScript in many ways: // creates an object which makes the Object, prototype of data. var data1 = new Object(); // Object literal notation; Object still is the prototype of data2. var data2 = {}; // anotherObject is now the prototype of data3. var data3 = Object.create(anotherObject); /* data3 is an object which can be verified bye typeof operator, however, it now has no prototype and you can build everything from scratch. */ var data3 = Object.create(null); But I don't know which versions of IE support the last method, i.e. Object.create(null) method? Matías

How to implement *object* for improve my clock sample javascript program

半城伤御伤魂 提交于 2019-12-05 23:41:18
问题 The goal of this work is to understand and play with meaning of some object concept I've heard arround. About the bounty There is a lot of different way / approach to do this. My tries are not really clean: for adding a 2st clock, with another timezone, I have to edit 3 different places. This is not well (see at bottom of the answer). How could I do something more useful ? In the begining: post-edit: the initial question was about choosing between jquery and mootools, now choice as been made;

Cannot read property 'style' of undefined — Uncaught Type Error

江枫思渺然 提交于 2019-12-05 21:35:41
问题 I would like to change the color, fontsize and font weight of the text in a span element of the html page. I am using the following code: if(window.location.href.indexOf("test") > -1){ var search_span = document.getElementsByClassName("securitySearchQuery"); search_span[0].style.color = "blue"; search_span[0].style.fontWeight = "bold"; search_span[0].style.fontSize = "40px"; } Following is the code for my html page <h1 class="keyword-title">Search results for<span class="securitySearchQuery">

Cloning A JavaScript Object - Including Getters and Setters

北城以北 提交于 2019-12-05 18:02:31
In a project I'm working on, I'm having to clone a object to a variable. I first tried - what seemed to be the most obvious solution - to do var obj2 = obj1 , however I soon realized this makes obj2 refer to obj1, so whenever I set a property in obj2, the property is updated in obj1, too. Well, I can't have that. So, I started searching around for ways to clone a object in JavaScript - I found multiple solutions for this, mainly var obj2 = JSON.parse(JSON.stringify(obj1)) - but that didn't keep all getters and setters I had defined for my object! The now most obvious solution to me seems to