Home >Web Front-end >JS Tutorial >How to determine whether there are parameters in an object in javascript
Javascript method to determine whether there are parameters in the object: use the hasOwnproperty() method, such as [var obj = {a: 1,b: 2,c: 3} console.log(obj.hasOwnProperty( 'a'));].
#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.
Suppose there is an object Object with many parameters in it, but now we need to determine whether a certain parameter exists in the current object. What should we do?
It’s actually very simple. Just use the hasOwnproperty() method. Let’s take a look!
var obj = { a: 1, b: 2, c: 3 } console.log(obj.hasOwnProperty('a')); // true 因为当前定义对象中含有此参数, 故返回值为 true
var obj = { a: 1, b: 2, c: 3 } console.log(obj.hasOwnProperty('asd')); // false 此时返回的就是false 因为当前对象中不包含此值,
Its syntax can also be seen obj.hasOwnProperty(prop)
All objects that inherit Object will inherit the hasOwnProperty method. This method can be used to check whether an object contains specific properties of its own; unlike the in operator, this method ignores properties inherited from the prototype chain.
Recommended learning: javascript video tutorial
The above is the detailed content of How to determine whether there are parameters in an object in javascript. For more information, please follow other related articles on the PHP Chinese website!