0

Suppose such a snippet of html

<a name="edit" href="javascript:" onclick="edit_column(this, {{ column.id }})"><span class="glyphicon glyphicon-pencil"></span></a>

the js:

function edit_column(the, column_id){
  alert(the)}

On clicking, it alerts "javascript:"

I tried to retrieve more info about it with

function edit_column(the, column_id){
  alert(typeof the)}

It alert object

How could I retrieve properties and method of such a specified object, like dir or vars in python?

1 Answer 1

2

You can use Object.keys to get an array of the object's enumerable own property names (and then use those to look up the values) or, in modern environments, Object.entries which gives you the names and values (as an array of arrays where each of the subordinate arrays is in the form [name, value]). If you need information about non-enumerable properties, there are also Object.getOwnPropertyNames and (if you need to look at its prototype) Object.getPrototypeOf.

But your specific example is a DOM element (an HTMLAnchorElement instance, to be precise), so you can also look up its properties and methods in the spec.

On clicking, it alerts "javascript:"

If you're wondering why that is, it's because alert coerces its argument to string, and the string an HTMLAnchorElement produces when you do that is its href property's value.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your tutorial, I tried alert(Object.keys(the)) , alert(Object.entries(the)) and alert(Object.entries($(the))), it prompt blank with no information.
@JawSaw - That doesn't surprise me, I think most DOM elements have only inherited properties, not own properties.
got it. I am doing searches to understand your comments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.