GithubHelp home page GithubHelp logo

Comments (12)

amitu avatar amitu commented on May 30, 2024

This is probably misnamed. Basic problem is I have if I define d = {} in rs-ng, d.hasOwnProperty() does not exist. If I do d = v'{}', d.hasOwnProperty exists.

Now React and other third party libraries expect .hasOwnProperty, and my rs-ng based code is littered with d = {} like constructs.

Should I replace d = v'{}' everywhere? It there a better way?

from rapydscript-ng.

kovidgoyal avatar kovidgoyal commented on May 30, 2024

You should never use hasOwnProperty() on a javascript object. Always do
Object.prototype.hasOwnProperty.call(obj, name)

I seriously doubt any well-written third party library expects an existence of hasOwnProperty -- that would be a big anti-pattern.

{} in RapydScript is Object.create(null)
If you want the JS {} then use v'{}' or Object.create(Object.prototype)

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

React does. React.createElement() method.

I request you to reopen this add a bool method on dict class .hasOwnProperty(). Of course in your time. I can even work on a patch if you give me some pointers.

from rapydscript-ng.

kovidgoyal avatar kovidgoyal commented on May 30, 2024

Then you should open a bug report with react and tell them to fix that. Otherwise, simple code like this will blow up:

React.createElement({'hasOwnProperty':1})

The point is that library code can never assume that objects passed to it have a member named hasOwnProperty that is the same as Object.prototype.hasOwnProperty

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

You should never use hasOwnProperty() on a javascript object. Always do Object.prototype.hasOwnProperty.call(obj, name) is not a javascript standard rule, it is an advise (a very good one, I agree), same way like don't use global variables.

It is not just React, jQuery, uses .hasOwnProperty on tons of places. Nearly all the examples given in MDN also show o.hasOwnProperty usage. They do warn about what you warn, but its just a warning, not error. Angular, dojo, Knockout, everyone uses it. I am afraid I must contradict you, your assertion that well written libraries don't do it is factually incorrect.

Far too much code written in wild depends on .hasOwnProperty(), I request you to reconsider your decision on this issue.

from rapydscript-ng.

kovidgoyal avatar kovidgoyal commented on May 30, 2024

IMO a library that does this is not well written, since by definition it uses a broken paradigm. :) However, I dont see the problem here, if you need a js object, use v'{}' instead of {}, it's not that much extra typing.

from rapydscript-ng.

kovidgoyal avatar kovidgoyal commented on May 30, 2024

Alternately, if you want syntactic sugar, use:

def jswrap(**kw):
   kw.hasOwnProperty = Object.prototype.hasOwnProperty.bind(kw)
   return kw

Then you can create js objects which have hasOwnProperty like this:

jswrap(x=1, y=2, z=3)

which looks just as nice as

{x:1, y:2, z:3}

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

foo

This is a bug. And I am afraid, not in jQuery. The fact that both d and y look almost exactly alike, but are completely different, to the extent of breaking jQuery/React/Angular/nearly every library that I checked, is bad for everyone who uses rs-ng.

If your dict is so different from javascript dict, then please at least change its toString() etc to make it obvious to developers to see what they are dealing with. I am afraid, inside the browser {} means one thing, and any object that is pretending to be {} but is completely incompatible, should not misuse .toString() like you are doing. The only clue I have that I am dealing with your version of something is by stopping the debugger, noticing that .prototype is missing, which is a major FAIL to me.

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

Even your ρσ_list instances somehow have .hasOwnProperty method.

Come on, reopen the bug. Tell me how to do it, and I will try to create a pull request. I am sure your dict is cool, abandoning it just because it does not implement one standard method is a bad tradeoff for everyone.

from rapydscript-ng.

kovidgoyal avatar kovidgoyal commented on May 30, 2024

You dont seem to understand the difference between an object and a hash. In rs-ng the {} notation creates a hash, which is a JS object with no prototype. If you want to create a javascript object with the default prototype, use v'{}'

The whole point of using rs-ng is that it is semantically close to python. Having {} generate a hash rather than an object is in line with that goal. If I were to change that, then people that are used to python will complain that:

a = {}
a['hasOwnProperty'] != undefined

And given that your rationale for having {} generate JS objects is based on broken behavior in poorly designed JS libraries, I am not convinced by it.

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

Regarding your code, it seems you are not getting what I am saying at all.

You have .keys() on your dict right? Does it mean you get:

a = {}
a['keys'] != undefined

?

In plain old javascript object, the .hasOwnProperty etc are implemented by using .prototype, not by adding it in the hash as you seem to mistakenly believe, looking at your example.

How many methods do you have on dict? Do they all show up in your dict? No.

I am not saying {} generate anything different than what you are generating right now. I am saying add one more method to what you are generating.

That method is very important method. Nearly every library in wild that takes data expects that method, this is a standard practice, and not a bug or poor design as you keep on claiming. Even MDN clearly states that sometimes, if the object has its own version of .hasOwnProperty(), the result may not be what you think, but this is exactly what happens when you override any method, this is like OOP101, if you override a method, the behaviour may change. It does not warn that some objects may not have this method at all, as that is not normal javascript, that is some non standard magic you have done.

The contract that this method would exist is a contract people rely on. Do not break it. Nothing will break.

One line probably you will have to add, thats all.

from rapydscript-ng.

amitu avatar amitu commented on May 30, 2024

Never mind all this. from __python__ import dict_literals fixed everything for me. Turns your dict() class is compatible with jQuery and standards. The javascript dict is compatible with jQuery and standards. Only the abomination that is used when you do not use the dict_literals when using rs-ng is buggy. Why do we have so many dicts? :-)

from rapydscript-ng.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.