extend

Extend a JavaScript object by passing a string with the path and a value

依然范特西╮ 提交于 2020-01-04 14:22:33
问题 Is there an easy way to extend a JavaScript object by passing a string and a value? Basically I need something like this: myObject = {} var extendObj = function(obj, path, value){ } var path = "a.b.c", value = "ciao"; extendObj(myObject, path, value); console.log(myObject.a.b.c) //will print "ciao" 回答1: myObject = {}; var extendObj = function (obj, path, value) { var levels = path.split("."), i = 0; function createLevel(child) { var name = levels[i++]; if(typeof child[name] !== "undefined" &&

ASP.NET MVC - Custom IIdentity or IPrincipal with Windows Authentication

六月ゝ 毕业季﹏ 提交于 2020-01-03 04:53:07
问题 I am working on an intranet site with Windows Authentication for logins. However, I want to extend the IPrincipal to have other properties. For instance, I'd like to get the user's FirstName in @User.FirstName or User.AuthorizedActivity("Admin/Permissions/Edit") (would retrieve from db) using activities instead of roles to hide certain links, etc. I am really having a heck of a time figuring this out over the past 2 days and find much information doing this with Windows Authentication. My

Recursive functions and lists appending/extending

最后都变了- 提交于 2020-01-01 10:14:00
问题 This is a very simple code in place of a bigger problem, but I'm hoping I can tackle it in chunks. I'll start with my first problem. def testrecurse(z,target): x=[] if z<target: z*=2 x.append(z) x.extend(testrecurse(z,target)) return x This is a test function to help my brain out with recursion. It takes a number, then shows all the multiplications of two until it hits the target number. so if I enter: testrecurse(1,1000) I receive: [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] which is great!

Package name alias or nickname

拈花ヽ惹草 提交于 2020-01-01 09:37:26
问题 Is it possible to create a nickname for a package name, that I can refer in xml file? I don't want to write the whole package name e.g. com.example.go.deck.today instead can I create an alias and refer that alias? I'd like to refer an alias or nick instead of the package name in an android/layout XML file. 回答1: Android by itself does not provide such facility. However you can use the ant to do it for you. You could write a custom build.xml, customizing the target <target name="-code-gen"> to

Should I subclass Python list or create class with list as attribute?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 05:07:12
问题 I need a container that can collect a number of objects and provides some reporting functionality on the container's elements. Essentially, I'd like to be able to do: magiclistobject = MagicList() magiclistobject.report() ### generates all my needed info about the list content So I thought of subclassing the normal list and adding a report() method. That way, I get to use all the built-in list functionality. class SubClassedList(list): def __init__(self): list.__init__(self) def report(self):

Prototype for private sub-methods

亡梦爱人 提交于 2019-12-31 06:49:32
问题 I have code that looks like this: var baseClass = function() { // CODE var subClass = function() { // MORE CODE } } Adding methods to baseClass is fine, I just use baseClass.prototype.newMethod = function () { // NEW CODE } My question is how should I add methods to subClass? Is the only way to simply make it a public method? ######## EDIT ############## OK so I've rearranged the code so the subClass is outside the baseClass. I pass in baseClass so subClass can still access the properties of

Prototype for private sub-methods

天涯浪子 提交于 2019-12-31 06:49:09
问题 I have code that looks like this: var baseClass = function() { // CODE var subClass = function() { // MORE CODE } } Adding methods to baseClass is fine, I just use baseClass.prototype.newMethod = function () { // NEW CODE } My question is how should I add methods to subClass? Is the only way to simply make it a public method? ######## EDIT ############## OK so I've rearranged the code so the subClass is outside the baseClass. I pass in baseClass so subClass can still access the properties of

Better way to call superclass method in ExtJS

我的梦境 提交于 2019-12-29 16:33:07
问题 All the ExtJS documentation and examples I have read suggest calling superclass methods like this: MyApp.MyPanel = Ext.extend(Ext.Panel, { initComponent: function() { // do something MyPanel specific here... MyApp.MyPanel.superclass.initComponent.call(this); } }); I have been using this pattern for quite some time and the main problem is, that when you rename your class then you also have to change all the calls to superclass methods. That's quite inconvenient, often I will forget and then I

Strange syntax of Number methods in JavaScript

匆匆过客 提交于 2019-12-29 01:39:31
问题 Take a look at the following code: Number.prototype.isIn = function () { for (var i = 0, j = arguments.length; i < j; ++i) { if (parseInt(this, 10) === arguments[i]) { return true; } } return false; }; var x = 2; console.log(x.isIn(1,2,3,4,5)); // <= 'true' console.log(2.isIn(1,2,3,4,5)); // <= Error: 'missing ) after argument list' Why is it that when it's a variable, the code works correctly yet when it is a number literal, it fails ? And also, strangely enough, why does the following line

Element.prototype in IE7?

懵懂的女人 提交于 2019-12-28 15:56:09
问题 I'm trying to extend all dom elements so i can get and remove their children. The function is below (works in FF and Chrome). Is there an equivalent in IE7 to extend the base dom object? if (!Element.get) { Element.prototype.get = function(id) { for (var i = 0; i < this.childNodes.length; i++) { if (this.childNodes[i].id == id) { return this.childNodes[i]; } if (this.childNodes[i].childNodes.length) { var ret = this.childNodes[i].get(id); if (ret != null) { return ret; } } } return null; } }