javascript-objects

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

蓝咒 提交于 2019-12-08 02:31:18
问题 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

Access public function from private function in JavaScript

泪湿孤枕 提交于 2019-12-07 15:44:26
I have recently started with OOP in JavaScript. And, I am allover confused with these things. I know JavaScript is entirely different from Java. But this is causing problem. What I am trying to implement: function myClass() { //Declare private variable var privateVar = ''; //To act as constructor privateFunction('Hello'); //Getter this.publicFunctionGet = function() { return privateVar; } //Setter this.publicFunctionSet = function(x) { privateVar = x; } function privateFunction(x) { this.publicFunctionSet(x); } } //Create object of myClass var me = new myClass(); alert(me.publicFunctionGet());

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

跟風遠走 提交于 2019-12-07 14:47:36
问题 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

How can I extend properly a JS object?

孤街浪徒 提交于 2019-12-07 11:34:32
问题 Let's say I have something like this in a file named main.js : function obj_name() {} obj_name.prototype = { foo : function() { alert('hi!'); }, foo2 : function() { alert('hi again!'); } } Now I am trying this way to expand the object in another file extend.js : obj_name.prototype = { newfoo : function() { alert('hi #3'); } } ... but the problem is that it will just work if I code it this way: obj_name.prototype.newfoo = function() { alert('hi #3'); } I guess this may be a noob question. I

Cloning A JavaScript Object - Including Getters and Setters

一个人想着一个人 提交于 2019-12-07 09:21:06
问题 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

How to avoid 'undefined' errors in nested objects [duplicate]

守給你的承諾、 提交于 2019-12-07 08:50:48
问题 This question already has answers here : Access Javascript nested objects safely (8 answers) Closed last year . I'm looking for some good strategies for avoiding errors in JavaScript when using dot notation to call the children of children in objects that may or may not exist. At the bottom of the code snippet below is an example of a solution that works, but is inelegant (at best). It would be great to see some native JavaScript solutions or even external libraries that can help avoid this

is “Simple Javascript Inheritance” by John Resig still ok?

ぐ巨炮叔叔 提交于 2019-12-07 08:48:55
问题 I've found http://ejohn.org/blog/simple-javascript-inheritance/ and it's exactly what I'm searching for but 'm wondering if it still works and if can cause any issue. 回答1: Many modern libraries use classical inheritance now. Its core method is the following (JavaScript Patterns by Stoyan Stefanov, page 127): function inherit(C, P) { var F = function () {}; F.prototype = P.prototype; C.prototype = new F(); C.uber = P.prototype; C.prototype.constructor = C; } Example of projects where it's used

Whats the best way to remove a property from nested javascript Object?

女生的网名这么多〃 提交于 2019-12-07 03:46:00
问题 I have a tree object as below, I am trying to remove the items array property if it's empty. I am not sure on the best approach to do this? I am thinking of looping through the key, check the property and then remove using delete myJSONObject[prop] ... Any thoughts / ideas are welcome? [{ text: "TreeRoot", items: [{ text: "Subgroup1", items: [] }, { text: "Subgroup2", items: [] }, { text: "Subgroup3", items: [], items: [{ text: "subgroup5", items: [{ text: "subgroup6", items: [{ text:

Get key value of dictionary by index in jQuery

那年仲夏 提交于 2019-12-06 20:05:40
问题 I have a javascript dictionary object which has a pre-set keys that are defaulted to 0 . Then I need to loop through the elements of this dictionary by index and use the value of the key to set its value. Below is my code to make things easier to understand: var _map = { 'severity-normal': 0, 'severity-minimal': 0, 'severity-moderate': 0, 'severity-severe': 0, 'severity-highly-severe': 0 }; mapSeverities: function () { for (var i = 0; i < _map.length; i++) { //get the key value, ex: severity

Passing objects from template to view using Django

守給你的承諾、 提交于 2019-12-06 15:49:55
I am trying to figure out the architecture for the following app: The user is presented with a table. Each table cell has several fields the user will be filling in. There is a general submit button: when clicked on all the input data (along with some calculated data per cell based on the input values) should pass to a Django view. Here are the following questions: Can I organize the data structure as a set of objects in a way that each object will correspond to a table cell, whereas the Master object, that will eventually be passed to the Django view, will be a set of those objects? If so,