hasOwnProperty的异常行为

时间:2019-05-03 13:55:29

标签: javascript node.js json if-statement hasownproperty

我正在编写一个函数,该函数检查对象是否包含诸如“ id”或“ serif:id”之类的键。不幸的是,这不能正常工作。

import UIKit
import BmoViewPager

struct RecipeHeading {
    var id: Int
    var text : String
}

private let mainColor = UIColor.lightGray

class ViewControllerPage2: UIViewController {

    var headingtry = ["Again", "Again"]

    var ReceipeHeading = [

        RecipeHeading(id: 1, text: "Bookmark"),
        RecipeHeading(id: 2, text: "share"),
        RecipeHeading(id: 3, text: "Print"),
        RecipeHeading(id: 4, text: "Download"),
        RecipeHeading(id: 5, text: "PDF Colour"),
        RecipeHeading(id: 6, text: "pepsi"),
    ]

    @IBOutlet weak var viewPagerNavigationBar: BmoViewPagerNavigationBar!
    @IBOutlet weak var viewPager: BmoViewPager!
    override func viewDidLoad() {

        super.viewDidLoad()
        viewPager.dataSource = self
        viewPagerNavigationBar.autoFocus = true
        viewPagerNavigationBar.viewPager = viewPager

        viewPager.layer.borderWidth = 1.0
        viewPager.layer.cornerRadius = 5.0
        viewPager.layer.masksToBounds = true
        viewPager.layer.borderColor = UIColor.white.cgColor

        viewPager.infinitScroll = true
        viewPager.dataSource = self
        viewPager.presentedPageIndex = 0

    }
}

extension ViewControllerPage2: BmoViewPagerDataSource {
    // Optional
    func bmoViewPagerDataSourceNaviagtionBarItemNormalAttributed(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> [NSAttributedString.Key : Any]? {
        return [


            // NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17.0),

            NSAttributedString.Key.font: UIFont(name: "Avenir Next", size: 17.0)!,

            NSAttributedString.Key.foregroundColor : UIColor.gray
        ]
    }
    func bmoViewPagerDataSourceNaviagtionBarItemHighlightedAttributed(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> [NSAttributedString.Key : Any]? {
        return [
            // NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17.0),
            NSAttributedString.Key.font: UIFont(name: "Avenir Next", size: 17.0)!,
            NSAttributedString.Key.foregroundColor : UIColor.black
        ]
    }
    func bmoViewPagerDataSourceNaviagtionBarItemHighlightedBackgroundView(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> UIView? {
        let view = UnderLineView()
        view.marginX = 2.0
        view.lineWidth = 1.0
        view.strokeColor = mainColor
        return view
    }



    // Required
    func bmoViewPagerDataSourceNumberOfPage(in viewPager: BmoViewPager) -> Int {
        return 6
    }
    func bmoViewPagerDataSource(_ viewPager: BmoViewPager, viewControllerForPageAt page: Int) -> UIViewController {
        switch page {
        case 0:

            if let vc = storyboard?.instantiateViewController(withIdentifier: "ViewControllerAgain") as? ViewControllerAgain {
                return vc

            }
        case 1:
            if let vc = storyboard?.instantiateViewController(withIdentifier: "DemoViewController1") as? DemoViewController1 {
                return vc
            }
        case 2:
            if let vc = storyboard?.instantiateViewController(withIdentifier: "DemoViewController2") as? DemoViewController2 {
                return vc
            }
        case 3:
            if let vc = storyboard?.instantiateViewController(withIdentifier: "DemoViewController3") as? DemoViewController3 {
                return vc
            }
        case 4:
            if let vc = storyboard?.instantiateViewController(withIdentifier: "DemoViewController4") as? DemoViewController4 {
                return vc
            }
        case 5:
            if let vc = storyboard?.instantiateViewController(withIdentifier: "DemoViewController5") as? DemoViewController5 {
                return vc
            }
        default:
            break
        }
        return UIViewController()
    }


    func bmoViewPagerDataSourceNaviagtionBarItemTitle(_ viewPager: BmoViewPager, navigationBar: BmoViewPagerNavigationBar, forPageListAt page: Int) -> String? {


        return "\(headingtry)"


    }

}

测试对象是:

function returnIdPreferSerifId(object) {
    if (object.hasOwnProperty('serif:id' === true)) {
        return object['serif:id'];
    } else if (object.hasOwnProperty('id' === true)) {
        return object.id;
    } else {
        console.log(object)
        console.log(object.hasOwnProperty('serif:id' === true))
        console.log(object.hasOwnProperty('id' === true))
        throw `ID not found in Layer 1!`;
    }
}

我希望该函数返回“ ska”,因为它是对象ID。 相反,控制台显示

{ 
  id: 'ska',
  d: 'M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z',
  style: 'fill:rgb(187,222,251);stroke:white;stroke-width:1.33px;' 
}

谢谢您的帮助!

2 个答案:

答案 0 :(得分:4)

您将括号放在错误的位置:

if (object.hasOwnProperty('serif:id' === true))

应该是

if (object.hasOwnProperty('serif:id') === true)

如果愿意,可以将=== true删除

if (object.hasOwnProperty('serif:id'))

答案 1 :(得分:1)

object.hasOwnProperty('serif:id' === true)的评估如下所示。

object.hasOwnProperty(false)
false //if false is not key of object.

您应该将=== false移到()之外

if (object.hasOwnProperty('serif:id') === true)

否则,您无需在if语句中将某些内容与true进行比较。

if (object.hasOwnProperty('serif:id'))